In this tutorial, you will learn three ways to compare two Strings in java.
- Using equal to operator (==)
- Using equals() method of String class
- compareTo() method of String class
1. Comparing two strings by using == operator
In the following program we are using equal to operator ==
to compare two strings. This operator returns true if the string references are equal else it returns false. The program is self explanatory, we have created two strings with the same value, when we compared them using ==, it returned true so the statement inside if statement got executed.
class JavaExample{ public static void main(String args[]){ String website="BeginnersBook"; String website2="BeginnersBook"; if(website == website2) System.out.println("Both Strings are Equal"); else System.out.println("Both Strings are Not Equal"); } }
Output:
Both Strings are Equal
Note: The == operator only compares the String references so if you are creating a new instance of the String class then you should not use == operator. It will return false, even if the string value is same. For this, you should use the equals() method of String class, which we have discussed below.
2. Compare two strings using equals() method
As I have mentioned in the Note in the above section, when we create a new instance of the String, we should not use == operator for comparing these strings as it only compares the references of the Strings. We should use the equals()
method for such scenarios.
In the following example we have two strings which are same but we have called String class constructor to create these strings. It created a new instance for both of these strings that is the reason, the == operator returned false, even though the strings are same. However the method returned true as expected.
public class JavaExample { public static void main(String[] args) { String name = new String("Chaitanya"); String name2 = new String("Chaitanya"); System.out.print("Comparison using equals() method: "); if(name.equals(name2)) System.out.println("Strings are Equal"); else System.out.println("Strings are Not Equal"); System.out.print("Comparison using == operator: "); if(name==name2) System.out.println("Strings are Equal"); else System.out.println("Strings are Not Equal"); } }
Output:
Comparison using equals() method: Strings are Equal Comparison using == operator: Strings are Not Equal
Example 2: Comparing two strings while ignoring the case
Method equals()
is a case-sensitive method, to compare the strings where we need to ignore the case, we can use an alternative method of String class, which is equalsIgnoreCase().
class JavaExample{ public static void main(String args[]){ String name="Carl"; String name2="CARL"; System.out.println(name.equals(name2)); System.out.println(name.equalsIgnoreCase(name2)); } }
Output:
false true
3. Java String compare: using compareTo() method
The compareTo() method compares the strings and returns an int value, if the strings are equal it returns 0 else it returns an int value (negative if string is less than other string, positive if string is greater than other string).
class JavaExample{ public static void main(String args[]){ String name="Steve"; String name2="Steve"; if(name.compareTo(name2)==0) System.out.println("Strings are equal"); else System.out.println("Strings are not equal"); } }
Output:
Strings are equal
Conclusion
We learned three ways to compare java strings. Lets summarise these points:
- You should use
equals()
method when you want to compare strings without ignoring case. If you do not want to consider the case for comparison then useequalsIgnoreCase()
. - Use
compareTo()
method when you do not want true or false as a result of string comparison, this is especially useful when you are sorting the strings in alphabetical order like dictionary. - == operator should only be used for string references comparison.
Leave a Reply