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 objectsIn java both == and equals() method is used to check the equality of two variables or objects. == is a relational operator which checks if the values of two operands are equal or not, if yes then condition becomes true. equals() is a method available in Object class and is used to compare objects for equality.
Difference between equals and == operator is that, == is used to check reference or memory address of the objects whether they point to the same location or not, and equals() method is used to compare the contents of the object e.g. in case of comparing String its characters, in case of Integer it's their numeric ...
The equals() method compares two strings, and returns true if the strings are equal, and false if not.
Both equals() and "==" operator in Java is used to compare objects to check equality but main difference between equals method and == operator is that one is method and other is operator. equals() is used to compare both primitive and objects while "==" is only used for objects comparison.
In general, the answer to your question is "yes", but...
.equals(...)
will only compare what it is written to compare, no more, no less.equals(Object o)
method of the closest parent class that has overridden this method. Object#equals(Object o)
method. Per the Object API this is the same as ==
; that is, it returns true if and only if both variables refer to the same object, if their references are one and the same. Thus you will be testing for object equality and not functional equality.hashCode
if you override equals
so as not to "break the contract". As per the API, the result returned from the hashCode()
method for two objects must be the same if their equals
methods show that they are equivalent. The converse is not necessarily true. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With