In java there are three ways to compare two strings.
Ways of String Comparison:
1. By == operator.
2. By equals() method.
3. By compareTo() method.
1. By == operator:
== operator compares the references of the string objects not the actual content of the string objects. It returns true if reference of the compared strings are equal, otherwise returns false.
Example:
StringComparisonExample1.java
/** * This program is used to show the use of == operator. * @author w3spoint. */ class TestString{ String str1 = "w3spoint"; String str2 = "w3spoint"; String str3 = new String("w3spoint"); /** * This method is used to compare strings using == operator. * @author w3spoint */ public void stringComparison(){ //return true, because str1 and str2 both refers to the //same instance created in String constant pool. System.out.println(str1 == str2); //return false, because str3 refers to the // instance created in nonpool. System.out.println(str1 == str3); } } public class StringComparisonExample1 { public static void main(String args[]){ //creating TestString object. TestString obj = new TestString(); //method call obj.stringComparison(); } } |
Output:
True false |
2. By equals() method:
equals method compares the actual content of string objects not the references. equals() is an object class method and string class overrides it. String class provides following format of equals() method.
a. public boolean equals(Object obj): Compares the string with the specified object. Returns true if actual content are equal, otherwise returns false.
b. public Boolean equalsIgnoreCase(String str): Compares the actual content of the string with the content of specified string. Returns true if two string’s content are equal ignoring case, otherwise returns false.
Example:
StringComparisonExample2.java
/** * This program is used to show the use of equals method. * @author w3spoint. */ class TestString{ String str1 = "w3spoint"; String str2 = "w3spoint"; String str3 = new String("w3spoint"); String str4 = "jai"; String str5 = "W3SPOINT"; /** * This method is used to compare strings using equals operator. * @author w3spoint */ public void stringComparison(){ //return true, because content are same. System.out.println(str1.equals(str2)); System.out.println(str2.equals(str3)); //return false, because content are not same. System.out.println(str2.equals(str4)); //return false, because content are not same //(differ in case). System.out.println(str2.equals(str5)); //return true, because content are same ignoring case. System.out.println(str2.equalsIgnoreCase(str5)); } } public class StringComparisonExapmle2 { public static void main(String args[]){ //creating TestString object. TestString obj = new TestString(); //method call obj.stringComparison(); } } |
Output:
true true false false true |
3.By compareTo():
compareTo() method compares the two strings lexicographically i.e. character by character. String class provides the following formats of compareTo() method.
 a. public int compareTo(String str)
 b. public int compareToIgnoreCase(String str)
It returns 0 if the argument string is equal to this string, less than 0 if this string is lexicographically less than the string argument and greater than 0 if this string is lexicographically greater than the string argument.
Example:
StringComparisonExample3.java
/** * This program is used to show the use of compareTo method. * @author w3spoint. */ class TestString{ String str1 = "w3spoint"; String str2 = "w3spoint"; String str3 = "jai"; String str4 = "sandy"; /** * This method is used to compare * strings using compareTo operator. * @author w3spoint */ public void stringComparison(){ //return 0, because content are same. System.out.println(str1.compareTo(str2)); //return greater than 0, because str1 //lexicographically greater than str2. System.out.println(str1.compareTo(str3)); //return less than 0, because str1 //lexicographically less than str2. System.out.println(str3.compareTo(str4)); } } public class StringComparisonExample3 { public static void main(String args[]){ //creating TestString object. TestString obj = new TestString(); //method call obj.stringComparison(); } } |
Output:
0 13 -9 |
Download this example.
Next Topic: String concatenation in java with example.
Previous Topic: String handling in java with example.