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 ?
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()
.==
can be used in many object types but you can useObject.equals
for any type , especially Strings and Google Map Markers.With respect to the String class:
The equals() method compares the "value" inside String instances (on the heap) irrespective if the two object references refer to the same String instance or not. If any two object references of type String refer to the same String instance then great! If the two object references refer to two different String instances .. it doesn't make a difference. Its the "value" (that is: the contents of the character array) inside each String instance that is being compared.
On the other hand, the "==" operator compares the value of two object references to see whether they refer to the same String instance. If the value of both object references "refer to" the same String instance then the result of the boolean expression would be "true"..duh. If, on the other hand, the value of both object references "refer to" different String instances (even though both String instances have identical "values", that is, the contents of the character arrays of each String instance are the same) the result of the boolean expression would be "false".
As with any explanation, let it sink in.
I hope this clears things up a bit.
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.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.