Java: String comparison

In this article, we will discuss various ways to compare two strings in Java

 

Various ways to compare 2 strings

  • Using String class’s
    1. equals() method
    2. equalsIgnoreCase() method
    3. compareTo() method
    4. compateToIgnoreCase() method
  • Using == operator

Java starter folks tends to use “==” operator for string comparison, but that’s not the right way to do. Instead, we can use String class’s methods to compare 2 strings

 

Let’s us explore one-by-one with example and explanation

1. equals() method of String class

  • String class’s equals() method compares the original content of 2 strings
  • It does the comparison character-by-character
  • It is case-sensitive
  • If characters are exactly same, then returns true
  • Otherwise, returns false

StringEqualsComparison.java

package in.bench.resources.string.handling;

public class StringEqualsComparison {

	public static void main(String[] args) {

		// declare local variables
		String player1 = "Hamilton";
		String player2 = "Hamilton";
		String player3 = new String("Hamilton");
		String player4 = "hamilton";

		// CASE 1 : equals() using string literal
		if(player1.equals(player2)) { // true
			System.out.println("Both String Literals are equal");
		}
		else {
			System.out.println("String Literals are NOT equal");
		}

		// CASE 2 : equals() using string literal & string object
		if(player1.equals(player3)) { // true
			System.out.println("String Literals & String object's values are equal");
		}
		else {
			System.out.println("String Literals & String object's values are NOT equal");
		}

		// CASE 3 : equals() using string literal with case difference
		if(player1.equals(player4)) { // false
			System.out.println("Both String Literals are equal");
		}
		else {
			System.out.println("String Literals are NOT equal, with case difference");
		}
	}
}

Output: 

Both String Literals are equal
String Literals & String object's values are equal
String Literals are NOT equal, with case difference

Download: StringEqualsComparison.java

 

2. equalsIgnoreCase() method of String class

  • String class’s equalsIgnoreCase() method compares the original content of 2 strings, ignoring the case differences whether it is in lowercase or uppercase
  • It is case-insensitive
  • It does the comparison character-by-character
  • If characters are exactly same ignoring case differences, then returns true
  • Otherwise, returns false

StringEqualsIgnoreCaseComparison.java

package in.bench.resources.string.handling;

public class StringEqualsIgnoreCaseComparison {

	public static void main(String[] args) {

		// declare local variables
		String player1 = "Hamilton";
		String player2 = "Hamilton";
		String player3 = new String("Hamilton");
		String player4 = "hamilton";

		// CASE 1 : equalsIgnoreCase() using string literal
		if(player1.equalsIgnoreCase(player2)) {// true
			System.out.println("Both String Literals are equal");
		}
		else {
			System.out.println("String Literals are NOT equal");
		}

		// CASE 2 : equalsIgnoreCase() using string literal & string object
		if(player1.equalsIgnoreCase(player3)) { // true
			System.out.println("String Literals & String object's values are equal");
		}
		else {
			System.out.println("String Literals & String object's values are NOT equal");
		}

		// CASE 3 : equalsIgnoreCase() using string literal with case difference
		if(player1.equalsIgnoreCase(player4)) { // true
			System.out.println("Both String Literals are equal, even though there is a case difference");
		}
		else {
			System.out.println("String Literals are NOT equal");
		}
	}
}

Output:

Both String Literals are equal
String Literals & String object's values are equal
Both String Literals are equal, even though there is a case difference

Download: StringEqualsIgnoreCaseComparison.java

 

3. compareTo() method of String class

  • String class’s compareTo() method compares 2 strings lexicographically
  • Unlike equals() method this doesn’t returns a boolean value, instead returns integer value
  • This comparison returns either zero or positive value (+ve) or negative value (-ve), after comparing 2 strings (say Strings str1 and st2)
  • Returns 0, if str1 == str2
  • Returns +ve value, if str1 > str2
  • Returns -ve value, if str1 < str2

StringCompareToComparison.java

package in.bench.resources.string.handling;

public class StringCompareToComparison {

	public static void main(String[] args) {

		// declare local variables
		String player1 = "Hamilton";
		String player2 = "Hamilton";
		String player3 = new String("Hamilton");
		String player4 = "hamilton";

		// CASE 1 : compareTo() using string literal
		if(player1.compareTo(player2) == 0) { // true
			System.out.println("Both String Literals are lexicographically equal");
		}
		else if(player1.compareTo(player2) > 0) { // false
			System.out.println("1st String is lexicographically greater than 2nd String");
		}
		else if(player1.compareTo(player2) < 0) { // false System.out.println("1st String is lexicographically lesser than 2nd String"); } // CASE 2 : compareTo() using string literal & string object if(player1.compareTo(player3) == 0) { // true System.out.println("String Literal and string object's values are lexicographically equal"); } else if(player1.compareTo(player3) > 0) { // false
			System.out.println("1st String is lexicographically greater than 2nd String");
		}
		else if(player1.compareTo(player3) < 0) { // false System.out.println("1st String is lexicographically lesser than 2nd String"); } // CASE 3 : compareTo() using string literal with case difference if(player1.compareTo(player4) == 0) { // false System.out.println("Both String Literals are lexicographically equal"); } else if(player1.compareTo(player4) > 0) { // false
			System.out.println("1st String is lexicographically greater than 2nd String");
		}
		else if(player1.compareTo(player4) < 0) { // true
			System.out.println("1st String is lexicographically lesser than 2nd String");
		}
	}
}

Output:

Both String Literals are lexicographically equal
String Literal and string object's values are lexicographically equal
1st String is lexicographically lesser than 2nd String

Download: StringCompareToComparison.java

 

4. compareToIgnoreCase() method of String class

  • String class’s compareToIgnoreCase() method compares 2 strings lexicographically, ignoring the case differences whether it is in lowercase or uppercase
  • This is very similar to compareTo() method with only difference is case insensitive
  • This comparison returns either zero or positive value (+ve) or negative value (-ve), after comparing 2 strings (say Strings str1 and st2)
  • Returns 0, if str1 == str2
  • Returns +ve value, if str1 > str2
  • Returns -ve value, if str1 < str2

StringCompareToIgnoreCaseComparison.java

package in.bench.resources.string.handling;

public class StringCompareToIgnoreCaseComparison {

	public static void main(String[] args) {

		// declare local variables
		String player1 = "Hamilton";
		String player2 = "Hamilton";
		String player3 = new String("Hamilton");
		String player4 = "hamilton";

		// CASE 1 : compareToIgnoreCase() using string literal
		if(player1.compareToIgnoreCase(player2) == 0) { // true
			System.out.println("Both String Literals are lexicographically equal");
		}
		else if(player1.compareToIgnoreCase(player2) > 0) { // false
			System.out.println("1st String is lexicographically greater than 2nd String");
		}
		else if(player1.compareToIgnoreCase(player2) < 0) { // false System.out.println("1st String is lexicographically lesser than 2nd String"); } // CASE 2 : compareToIgnoreCase() using string literal & string object if(player1.compareToIgnoreCase(player3) == 0) { // true System.out.println("String Literal and String object's values are lexicographically equal"); } else if(player1.compareToIgnoreCase(player3) > 0) { // false
			System.out.println("1st String is lexicographically greater than 2nd String");
		}
		else if(player1.compareToIgnoreCase(player3) < 0) { // false System.out.println("1st String is lexicographically lesser than 2nd String"); } // CASE 3 : compareToIgnoreCase() using string literal with case difference if(player1.compareToIgnoreCase(player4) == 0) { // true System.out.println("Both String Literals are lexicographically equal"); } else if(player1.compareToIgnoreCase(player4) > 0) { // false
			System.out.println("1st String is lexicographically greater than 2nd String");
		}
		else if(player1.compareToIgnoreCase(player4) < 0) { // false
			System.out.println("1st String is lexicographically lesser than 2nd String");
		}
	}
}

Output:

Both String Literals are lexicographically equal
String Literal and String object's values are lexicographically equal
Both String Literals are lexicographically equal

Download: StringCompareToIgnoreCaseComparison.java

 

5. Using == operator

As we discussed in the beginning, Java starter tends to use == operator for string comparison. Actually, it does comparison but their reference address not with their original contents

  • == operator compare the references rather original string contents
  • When we define 2 string literal and compare using == operator, then it return true because of String pool concept
  • But when 2 string objects is compared, then it returns false as every individual objects is stored separately in heap memory even if the string contents is exactly same

StringEqualToEqualToOperatorComparison.java

package in.bench.resources.string.handling;

public class StringEqualToEqualToOperatorComparison {

	public static void main(String[] args) {

		// declare local variables
		String player1 = "Hamilton";
		String player2 = "Hamilton";
		String player3 = new String("Hamilton");

		// CASE 1 : equals() using string literal
		if(player1 == player2) { // true
			System.out.println("Both String Literals are equal, since both refers to same instance");
		}
		else {
			System.out.println("String Literals are NOT equal");
		}

		// CASE 2 : equals() using string literal & string object
		if(player1 == player3) { // false
			System.out.println("String Literals & String object's values are equal");
		}
		else {
			System.out.println("String are NOT equal");
		}
	}
}

Output:

Both String Literals are equal, since both refers to same instance
String are NOT equal

Download: StringEqualToEqualToOperatorComparison.java

 

References:

https://docs.oracle.com/javase/tutorial/java/data/comparestrings.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Read Also:

  • Java: String class
  • Java: String Literal and String constant pool concept
  • Java: String is Immutable
  • Java: String comparison
  • Java: String concatenation

 

Happy Coding !!
Happy Learning !!