The String Comparison in Java is one of the most commonly asked questions in the interview. The Java String Comparison may twist as differences between equals Method and == Operator.
In this article, we will show how to Perform String Comparison in Java Programming Language. Before we get into a practical example, let us see the major differences between them:
- The equals Method compares the string data with user-specified Object data to check whether they both represent the same sequence of characters or not. In short, it will compare the string values. Based on the result, it will return Boolean True or False.
- == Operator compares the Object reference of one string with another and returns the result.
How to Perform String Comparison in Java?
In this Java program, we are going to perform a string comparison. Here, we will compare two strings using both the equals method and the == operator.
package FAQs; public class StringComparison { public static void main(String[] args) { String str1 = "Tutorial Gateway"; String str2 = "Tutorial Gateway"; String str3 = new String("Java Programming"); String str4 = new String("Java Programming"); String str5 = new String("Tutorial Gateway"); String str6 = null; boolean a = str1.equals(str2); boolean b = str3.equals(str4); boolean c = str1.equals(str5); System.out.println("equals Method Result: str1 Equals to str2 = " + a); System.out.println("== Result : str1 Equals to str2 = " + (str1 == str2)); System.out.println("equals Method Result: str3 Equals to str4 = " + b); System.out.println("== Result : str3 Equals to str4 = " + (str3 == str4)); System.out.println("equals Method Result: str1 Equals to str5 = " + c); System.out.println("== Result : str1 Equals to str5 = " + (str1 == str5)); System.out.println("== Result : Null Equals to Null = " + (null == null)); System.out.println("equals Method Result: Null Equals to Null = " + str6.equals(null)); } }

Within this Java string comparison example program, we declared three variables, str3, str4, and str5, and assigned the corresponding values using the following six statements.
The following statement will call the equals Method method to compare the str1 with str2. From the above screenshot, if you observe that it is returning True because it is an Object.
boolean a = str1.equals(str2);
This Java String example will perform a comparison of the literal str1 with str2. From the above screenshot, it is returning True because both of them refer to the same object in memory.
System.out.println("== Result : str1 Equals to str2 = " + (str1 == str2));
The following Java statements will perform a comparison of str3 with another string, str4. From the above screenshot, observe that.
- The first statement returns True because the value in both str3 and str4 is the same.
- The second statement returns False. Though the value is the same == operator is returning False because str3 is creating a new object in Cache and str4 is creating another new object in Cache. It means the object reference of str3 and str4 is different. So, it is returning False.
boolean b = str3.equals(str4); System.out.println("== Result : str3 Equals to str4 = " + (str3 == str4));
The below statement in this Java String comparison example will differentiate the literal str1 from object str5.
- The first statement returns True because the value in both str1 and str5 is the same.
- The second Java statement returns False. Though the value is the same == operator is returning False because str1 is creating an object inside the memory and str5 is creating a new object in Cache. It means the object reference of str1 and str5 is different. So, it is returning False.
boolean b = str1.equals(str5); System.out.println("== Result : str1 Equals to str5 = " + (str1 == str5));
In the last program line, we are comparing the null values using this == operator. If we use the equals method to differentiate the null values, it will throw an error.