How to Compare Two Strings in Java
How to Compare Two Strings in Java
On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting (by compareTo() method), and authentication (by equals() method). Following are the three ways to compare two strings in Java.
1) By using == operator
2) By using compareTo() method
3) By using equals() method
1) By Using == Operator
The == operator makes the comparison of references, not values. Let’s understand it with the help of the following program.
FileName: StringComparisonExample.java
public class StringComparisonExample { // main method public static void main(String argvs[]) { // Input strings String str1 = "Tutorial & Example"; String str2 = "Tutorial & Example"; if(str1 == str2) // true, because st1 & str2 have same reference { System.out.println("Strings have the same reference."); } else { System.out.println("Strings do not have the same reference."); } } }
Output:
Strings have the same reference.
Explanation: Whenever a string is directly assigned to a variable, an object for the assigned string is implicitly created inside the string pool. When the same string is again assigned to another variable, the JVM (Java Virtual Machine) checks whether the object of the string is already exists in the string pool or not. If object is already present, the JVM returns the reference of the created object; otherwise, it creates an object for the assigned string.
In our example, the exact string is assigned twice. Therefore, both the variables str1 and str2 point to the same object, which is created in the string pool. Hence, the if condition returns true, and its print statement is executed.
Now, consider the following program.
FileName: StringComparisonExample1.java
public class StringComparisonExample1 { // main method public static void main(String argvs[]) { // Input strings String str1 = "Tutorial & Example"; // Creating a string object explicitly String str2 = new String("Tutorial & Example"); if(str1 == str2) { System.out.println("Strings have the same reference."); } else // false, because st1 & str2 do not have same reference { System.out.println("Strings do not have the same reference."); } } }
Output:
Strings do not have the same reference.
Explanation: In this example, the str2 variable is holding the reference of the object that is explicitly created. The explicitly created string objects do not reside in the string pool. Therefore, the references held by the variables str1 and str2 are different. Hence, this time the print statement of the else block executes.
2) By Using compareTo() Method
The compareTo() method does the comparison of string lexicographically and renders an integer value that represents whether the first string is either greater than, or less than, or equal to the second string.
Syntax:
public int compareTo (String anotherStr)
anotherStr: represents the string that is being compared with the current string.
An integer value is returned depending on the current string is greater than, or less than, or equal to the anotherStr string.
FileName: StringComparisonExample2.java
public class StringComparisonExample2 { // main method public static void main(String argvs[]) { // Input strings String str1 = "Rohit Gurunath Sharma"; String str2 = "Mahendra Singh Dhoni"; String str3 = "Mahendra Singh Dhoni"; String str4 = "Mahi"; // compareTo() method is invoked inside the print statements System.out.println( "The comparison result is: " + str1.compareTo(str2) ); System.out.println( "The comparison result is: " + str2.compareTo(str1) ); System.out.println( "The comparison result is: " + str2.compareTo(str3) ); System.out.println( "The comparison result is: " + str4.compareTo(str3) ); } }
Output:
The comparison result is: 5 The comparison result is: -5 The comparison result is: 0 The comparison result is: 4
Explanation: The first print statement gives 5 because the ASCII value of ‘R’ is 5 more than ‘M’. The second print statement gives -5 because the ASCII value of ‘M’ is 5 less than ‘R’. The third print statement gives 0 because the contents of str2 and str3 are exactly the same. For the last print statement, the first three letters of the word “Mahi” and “Mahendra” are the same. Therefore, the result is 0 till this point. However, the fourth letter of the word “Mahi”, i.e. the letter i, gets compared with the fourth letter, i.e. the letter e, of the word “Mahendra”. Here, the difference in the ASCII values of the letter is 4, which is being displayed in the output.
3) By Using String.equals() Method
The equals() method compares the content of the strings and returns true value when there is a complete match; otherwise, false value. Note that it overrides the equals() method of the Object class.
Syntax:
public boolean equals (Object anObject) anObject: represents the string that is being compared with the current string. A boolean value is returned depending on the current string is either equal to the anotherStr string or not. FileName: StringComparisonExample3.java public class StringComparisonExample3 { // main method public static void main(String argvs[]) { // Input strings String str1 = "Rohit Gurunath Sharma"; String str2 = "Mahendra Singh Dhoni"; String str3 = "Mahendra Singh Dhoni"; String str4 = "mahendra singh dhoni"; // equals() method is invoked inside the print statements System.out.println( "The comparison result is: " + str1.equals(str2) ); System.out.println( "The comparison result is: " + str2.equals(str1) ); System.out.println( "The comparison result is: " + str2.equals (str3) ); System.out.println( "The comparison result is: " + str3.equals (str4) ); } }
Output:
The comparison result is: false The comparison result is: false The comparison result is: true The comparison result is: false
Explanation: The first print statement gives 5 because the ASCII value of ‘R’ is 5 more than ‘M’. The second print statement gives -5 because the ASCII value of ‘M’ is 5 less than ‘R’. The third print statement gives 0 because the contents of str2 and str3 are precisely the same.