Difference between equals operator == and equals() method

For comparison of two variable for their equality Java offers two methods.

  1. Equals operator ==, and
  2. equals() method in Object class.

Since both of them checks the equality of L.H.S. and R.H.S. then what is the difference between them?

Difference lies in the definition

The difference lies in the definition of the operator and the method. The definition of == says that the operator compares the value inside the variable available on both sides. And the definition of the equals() method changes as per the implementation in the corresponding class.




To understand the difference take a scenario of String objects. We have three String variables viz. s1, s2 and s3.

Based on the above initialization, what would be the output of follow statements? One thing is sure, these statements will compile and each will generate a boolean output.

If you think you have the answer and possible output of the above statements, then write it down somewhere. Because we will verify it in few moments.

To verify the output you guessed (hopefully based on some concept) check if you were thinking about the following questions or not. (If not, then you should have.)

  1. What is stored in s1, s2 and s3?
  2. What new operator does?
  3. What equals() method of String class does?

What is stored in s1, s2 and s3?

When an object is initialized, the variable stores the memory address (reference) of the value. This is why they are also called Reference variables. And, strings literals are immutable and are created inside String Constant Pool, a pool of all the Strings. So, s1 and s2 will hold the memory address (reference) of the "hello" (available inside String Constant Pool). In other words, same reference to the string literal.

This answers the statement (1) output. The statement (1) will print true, because s1 and s2 contains same reference to the string literal “hello”.

What about s3? Answer to this question lies in the answer of following question.

What new operator does?

new operator is used to create and allocate new memory for a newly created object. So, String s3 = new String("hello"); will create a new object of String type and the reference will be stored in s3. Since, the reference is newly created, then it is (not by any chance) will be same as available in s1 or s2.

Statement (2) is answered by the answer to this question. It will print false. Because of the fact that s3 will hold the reference of newly created instance of “hello”, which will be different from the reference held in s1 (or s2).




What equals() method of String class does?

equals() method of String class does following operations before returning a boolean value.

  1. Check if both instance contains same reference using ==.
  2. Check if the reference passed in parameter is of String type.
  3. Check the length of both instances (this and passed in parameter).
  4. Compare both strings character-by-character to check, if both contains same characters

Based on the above mentioned checks, statements (3) and (4) will print true. Because, in short, String class overrides equals() and performs check on the value available the reference rather than comparing the reference values only.

Sample Program

A sample program is also available on GitHub. You can refer to the file using this link.