Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between == operator and equals()? (with hashcode() ???)

I was learning hashcode in more depth and figured that:

1. If you override equals(), you must override hashcode() too.

2. To find if 2 objects are same object, use == operator

Given those 2 factors, in Java I was assuming that when == operator is used to compare if 2 instances are same or not,

if(object1 == object2)

is actually doing

if(object1.hashcode() == object2.hashcode())

But it appears I was wrong by running the test below.

public class Main {

    public static void main(String[] args){
        Obj1 one = new Obj1();
        Obj1 two = new Obj1();
        //is this calling hashCode() in backend???
        if(one == two) {
            System.out.println("same");
        }
        else {
            System.out.println("nope");
        }
        //this is of course return true
        if(one == one) {
            System.out.println("one and one is same");
        }
    }
}

class Obj1 {
    @Override
    public int hashCode() {
        System.out.println("hashCode() is called");
        return 111;
    }
    @Override
    public boolean equals(Object another) {
        System.out.println("equals() is called");
        return false;
    }
}

According to the test which uses == operator and see if equals() is called and it wasn't.

So my question is if == operator can used to compare if the object is same or not, what is the point of overriding equals() and hashCode() method for comparison? Isn't == operator do the job already?

reference:

Overriding hashCode() - is this good enough?

http://mindprod.com/jgloss/hashcode.html

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#equals(java.lang.Object)

like image 425
Meow Avatar asked Dec 06 '22 00:12

Meow


2 Answers

the == operator determines if 2 references point to the same object.

So

 Object o1 = new Object();
 Object o2 = o1;

 o1 == o2; //true

 o2 = new Object();

 o1 == o2 // false

the Object.equals() method is "how do I determine if 2 references to objects, that are not the same object, are equal?"

If two references point to the same object, both

o1 == o2 
o1.equals(o2) 

should be true.

But if o1 and o2 are not the same object, they still might be equal logically. For any given class, equals depends on the semantics behind the object. For example, consider a class where field1 and field2 are set by the user, but field3 is computed and has a random element to its computation. It might make sense to define equals in this case to only depend on field1 and field2, and not field3. Thats why equals is necessary.

like image 173
hvgotcodes Avatar answered Dec 08 '22 15:12

hvgotcodes


== is identity.

.equals() is equality.

.equals() defaults to just using == (just like hashCode() defaults to System.identityHashCode() but you can override them if there's a more meaningful way to check for equality. Typically this is a sort of "structural" equality. ie: are all of the pieces of this .equal() to all of the pieces of that?

like image 27
Laurence Gonsalves Avatar answered Dec 08 '22 14:12

Laurence Gonsalves