Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between == and equals() in Java?

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
like image 243
brainydexter Avatar asked Sep 22 '11 19:09

brainydexter


People also ask

What is the difference between == and equals () method in Java?

In 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.

What is the difference between equals () and == in Java Quora?

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 ...

What is equal () in Java?

The equals() method compares two strings, and returns true if the strings are equal, and false if not.

What is difference between equals and == in Java Javatpoint?

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.


2 Answers

In general, the answer to your question is "yes", but...

  • .equals(...) will only compare what it is written to compare, no more, no less.
  • If a class does not override the equals method, then it defaults to the equals(Object o) method of the closest parent class that has overridden this method.
  • If no parent classes have provided an override, then it defaults to the method from the ultimate parent class, Object, and so you're left with the 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.
  • Always remember to override 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.
like image 178
Hovercraft Full Of Eels Avatar answered Sep 19 '22 09:09

Hovercraft Full Of Eels


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.

like image 29
Jacques Colmenero Avatar answered Sep 19 '22 09:09

Jacques Colmenero