Skip to content

Instantly share code, notes, and snippets.

@LeoHeo
Last active June 10, 2018 04:12
Show Gist options
  • Select an option

  • Save LeoHeo/a1327a68a5eb2a70346ddfb9bdd0a3c5 to your computer and use it in GitHub Desktop.

Select an option

Save LeoHeo/a1327a68a5eb2a70346ddfb9bdd0a3c5 to your computer and use it in GitHub Desktop.

Revisions

  1. LeoHeo revised this gist Jun 10, 2018. 2 changed files with 14 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion intellij_equals.java → 1_intellij_equals.java
    Original file line number Diff line number Diff line change
    @@ -7,5 +7,5 @@ public boolean equals(Object o) {
    return false;
    }
    Student student = (Student) o;
    return Objects.equals(getId(), student.getId());
    return Objects.equals(getId(), student.getId()); // Java7+ 부터 사용할 수 있는 `Objects.equals`를 사용해봤다.
    }
    13 changes: 13 additions & 0 deletions 2_overideEquals.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    package io.github.leoheo.equals;

    public class Main {

    public static void main(String[] args) {
    Student leo1 = new Student(1L, "Leo");
    Student leo2 = new Student(1L, "Leo");

    System.out.println("leo1 hashcode => " + leo1.hashCode()); // 1625635731
    System.out.println("leo2 hashcode => " + leo2.hashCode()); // 1580066828
    System.out.println("leo1.equals(leo2) => " + leo1.equals(leo2)); // true
    }
    }
  2. LeoHeo created this gist Jun 10, 2018.
    11 changes: 11 additions & 0 deletions intellij_equals.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    @Override
    public boolean equals(Object o) {
    if (this == o) {
    return true;
    }
    if (!(o instanceof Student)) {
    return false;
    }
    Student student = (Student) o;
    return Objects.equals(getId(), student.getId());
    }