I wanted to clarify if I understand this correctly:
==
-> is a reference comparison, i.e. both objects point to the same memory location.equals()
-> evaluates to the comparison of values in the objects
Am I correct in my understanding ?
I wanted to clarify if I understand this correctly:
==
-> is a reference comparison, i.e. both objects point to the same memory location.equals()
-> evaluates to the comparison of values in the objectsAm I correct in my understanding ?
It may be worth adding that for wrapper objects for primitive types - i.e. Int, Long, Double - == will return true if the two values are equal.
To contrast, putting the above two Longs into two separate ArrayLists, equals sees them as the same, but == doesn't.
Basically,
==
compares if two objects have the same reference on the heap, so unless two references are linked to the same object, this comparison will be false.equals()
is a method inherited fromObject
class. This method by default compares if two objects have the same referece. It means:object1.equals(object2)
<=>object1 == object2
However, if you want to establish equality between two objects of the same class you should override this method. It is also very important to override the method
hashCode()
if you have overridenequals()
.Implement
hashCode()
when establishing equality is part of the Java Object Contract. If you are working with collections, and you haven't implementedhashCode()
, Strange Bad Things could happen:null
will be printed after executing the previous code if you haven't implementedhashCode()
.The == operator:
The == is a relational operator in Java that is used to compare two operands. It is used to determine whether the two operands are equal or not. Using the == operator, you can compare any primitive type such as int, char, float and Booleans. After comparison, the == operator returns a boolean value. If the two operands are equal, the == operator returns a true value. However, if the two operands are not equal, it returns a false value. When used with objects, the == operator compares the two object references and determines whether they refer to the same instance.
The .equals() Method
equals() is a method available in the String class that is used to compare two strings and determine whether they are equal. This method returns a boolean value as a result of the comparison. If the two strings contain the same characters in the same order, the equals() method returns true. Otherwise, it returns a false value.
When you evaluate the code, it is very clear that (==) compares according to memory address, while equals(Object o) compares hashCode() of the instances. That's why it is said do not break the contract between equals() and hashCode() if you do not face surprises later.
In short, the answer is "Yes".
In Java, the
==
operator compares the two objects to see if they point to the same memory location; while the.equals()
method actually compares the two objects to see if they have the same object value.Cheers :-)