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.


(adsbygoogle = window.adsbygoogle || []).push({});

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

String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");

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.

System.out.println(s1 == s2);       // (1)
System.out.println(s1 == s3);       // (2)
System.out.println(s1.equals(s2));  // (3)
System.out.println(s1.equals(s3));  // (4)

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


(adsbygoogle = window.adsbygoogle || []).push({});

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.

public class StringDemo {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        String s3 = new String("hello");
        // true : value (address of "hello" from String constant pool) stored in s1 is same as in s2
        System.out.println(s1 == s2);

        // false : new address is generated for s3 which is not same as the value in s1
        System.out.println(s1 == s3);

        // true : value at address contained in s1 and s2 are same
        System.out.println(s1.equals(s2));

        // true : value at address contained in s1 and s3 are same
        System.out.println(s1.equals(s3));
    }
}