String Comparison in Java

The String Comparison in Java is one of the most commonly asked questions in interviews. The Java String Comparison may twist as differences between Java equals Method and == Operator.

In this article, we will show how to Perform String Comparison in Java Programming Language. Before we get into a Java String Comparison practical example, let us see the major differences:

  • The Java equals Method compare 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, Java 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 Java string comparison. Here, we will compare two strings using both string.equals method and == operator.

// Example program to perform String Comparison in Java
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));
	}
}
String Comparison in Java 1

Within this Java String Comparison example, we declared three String Objects str3, str4, str5, and assigned corresponding values using the following statement.

String str3 = new String("Java Programming");
String str4 = new String("Java Programming");
String str5 = new String("Tutorial Gateway");

The following statement will call the Java equals Method method to compare the string str1 with str2. From the above screenshot, observe that it is returning True because String is an Object.

boolean a = str1.equals(str2);

In this Java String Comparison example, the following statement will compare the string 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 statements will compare the string Object str3 with another string object str4. From the above screenshot, observe that

  • The first statement returns True because the string value in both str3 and str4 is the same.
  • The second statement returns False. Though the string 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 compare the string literal str1 with String object str5. From the above screenshot, observe that

  • The first statement returns True because the string value in both str1 and str5 is the same.
  • The second Java statement returns False. Though the string 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 Java program line, we are comparing the null values, use this == operator. If we use the equals method to compare the null values, it will throw an error.

System.out.println("== Result           : Null Equals to Null = " + (null == null));