[Java] Let’s override the difference between == (identity) and the equals method (equivalence)

2 minute read

There are two “same” in java. #== (identity) The == operator determines that they are the same. That is, it is determined that a plurality of variables refer to the same instance.


A a = new A(); // create an instance of type A
A b = a; // Copy each instance of a to b as is
System.out.println(a == b);
true

example


public class Sample {
private int num;
public Sample(int num) {
this.num = num;
}
}
public class Main {
public static void main(String[] args) {
Sample s0 = new Sample(5); // Instantiation of Sample type
Sample s1 = s0; // copy to s1
s0 = new Sample(5); // Create a new Sample type instance
System.out.println(s0 == s1);
}
}
false

When I copied it with Sample s1 = s0;, it had the same instance, but in the next line s0 created a new instance, and s0 and s1 became different instances.

equals method (equivalence)

The equals method determines that they are the same value. That is, it is determined whether the instances have the same value, though different.

The equals method defined in the default Object class is

public boolean equals(Object obj){
  return (this == obj);
}

Since it is defined as, and it is a specification to confirm the sameness,

** It is assumed to be used by overriding. **

further! !!

**When overriding the equals method, you must also override hashCode in the set. **

  • HashCode is a method that you can check by converting the value that the instance has into a hash function.

Object(Java SE 11 & JDK 11)

equals Keep in mind that if you generally override this method, you should always override the hashCode method and follow the general contract of hashCode methods that equivalent objects should hold equivalent hash codes.

example


public class Sample {
private int num;
private String name;
public Sample(int num, String name) {
this.num = num;
this.name = name;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ?0: name.hashCode());
result = prime * result + num;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Sample other = (Sample) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (num != other.num)
return false;
return true;
}


}


public class Main {
public static void main(String[] args) {
        // arguments (num, name) are the same
Sample a = new Sample(5, "xxx");
Sample b = new Sample(5, "xxx");
System.out.println(a.equals(b));
        // output each hashCode
        System.out.println(a.hashCode());
        System.out.println(b.hashCode());
}
}
true
3694926
3694926

Overrides equals method and hashCode method.

If any of num or name is different, it will be as follows.


public class Main {
public static void main(String[] args) {
        // The argument (name) is different
Sample a = new Sample(5, "xxx");
Sample b = new Sample(5, "yyy");
System.out.println(a.equals(b));
        // output each hashCode
        System.out.println(a.hashCode());
        System.out.println(b.hashCode());
}
}
false
3694926
3725709

The equals judgment is set to false and a different hashCode value is output.