The operator, ==, tests to see if two object reference variables refer to the exact same instance of an object.
The method, .equals(), tests to see if the two objects being compared to each other are equivalent -- but they need not be the exact same instance of the same object.
Example #1:
Integer i = new Integer(10);
Integer j = i;
in the above code. i == j
is true because both i
and j
refer to the same object.
Example #2:
Integer i = new Integer(10);
Integer j = new Integer(10);
In the above code, i == j
is false because, although they both have the value 10, they are two different objects.
Also, in the above code, i.equals(j)
is true because although they are two different objects, they are equivalent in the fact that they represent the same number, 10.