To understand Java String equals() method, let’s first understand the default implementation of equals() method in Object class.
Every class in Java extends Object class by default. Object class is the default Superclass. Object class contains equals() method and String class overrides equals() method implementation.

What is default implementation of equals method in Object class?

Default implementation of equals() method in Object class checks for reference equality i.e. two objects are are equal if their references point to the same object.

The default implementation in Object class is as follows:

    public boolean equals(Object obj) {
        return (this == obj);
    }

How equals() method in String differs from Object class equals() method??

equals() method is overridden in String class and it checks for content equality and not reference equality.
Let’s see what is the default implementation in String class.

    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

We can see here that each character of two strings are checked for equality in String equals() method.

e.g. Suppose there are two objects of String as shown below

String str1 = new String(“Hello”);
String str2 = new String(“Hello”);
System.out.println(str1.equals(str2)); //true as it checks content equality

This will print true as although these objects are different but their contents are equal. It depends only on the content and not on the references to the objects.

Let’s take few examples with respect to java String equals() method:

Example 3: java String equals() method

package com.masterjavatutorial;
public class Test {
    public static void main(String[] args) {
	String str = "HelloWorld";
	String str1 = new String("HelloWorld");
	System.out.println(str == str1); 
	System.out.println(str.equals(str1));  		
    }
}

Output of the above program is as below:

false
true

Example 2: java String equals() method

package com.masterjavatutorial;
public class Test {
	public static void main(String[] args) {
	  String str = new String("HelloWorld");
	  String str1 = new String("HelloWorld");
	  System.out.println(str == str1); 
          System.out.println(str.equals(str1));  		
	}
}

Output of the above program is as below:

false
true

Example 3: java String equals() method

package com.masterjavatutorial;
public class Test {
	public static void main(String[] args) {
	   String str = "HelloWorld";
	   String str1 = "HelloWorld";
	   System.out.println(str == str1); 
           System.out.println(str.equals(str1));  		
	}
}

Output of the above program is as below:

true
true

Leave a Reply