The String Comparison in Java is one of the most commonly asked question in interviews. It may be twisted as, differences between Java equals Method and == Operator.
In this article we will show you, How to Perform String Comparison in Java Programming Language. Before we get into practical example, let us see the major difference:
- 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 shortly, it will compare the string values and 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 Java string comparison. Here, we will compare two strings using both string.equals method and == operator.
JAVA CODE
// 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)); } }
OUTPUT
ANALYSIS
Within this Java String Comparison example, First, we declared two String literal str1, str2 and assigned corresponding values using following statement
String str1 = "Tutorial Gateway"; String str2 = "Tutorial Gateway";
Next, we declared three String Objects str3, str4, str5 and assigned corresponding values using following statement
String str3 = new String("Java Programming"); String str4 = new String("Java Programming"); String str5 = new String("Tutorial Gateway");
Following statement will call the public boolean equals (Object Obj) method to compare the string str1 with str2. From the above screenshot you can 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 you can observe that, it is returning True because both of them refers to 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 you can observe that,
- First statement returns True because the string value in both str3 and str4 are same.
- Second statement returns False. Though the string value is same == operator is returning False because, str3 is creating 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));
Below statement in this Java String Comparison example will compare the string literal str1 with String object str5. From the above screenshot you can observe that,
- First statement returns True because the string value in both str1 and str5 are same.
- Second statement returns False. Though the string value is same == operator is returning False because, str1 is creating object inside the memory and str5 is creating 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));
When you are comparing the null values you can use this == operator.
System.out.println("== Result : Null Equals to Null = " + (null == null));
NOTE: If you use the equals method to compare the null values, it will throw error as we shown in above screenshot
Thank You for Visiting Our Blog