Java Compare Strings Best Practice


The java.lang.String class is one of the most used classes in the java world. So applying the best practices regarding strings can have a big impact on your code quality and performance.

Comparing strings is something needed in every application, So How to do string comparison in Java? We will see different ways to Compare two strings characters in Java to determine if they are equal, otherwise which string is greater than the other.

  • Compare strings values using String.equals()
    • Compare strings Alphabetically using String.equalsIgnoreCase()
  • Java String comparison using String.compareTo() Method
    • Java String comparison using String.compareToIgnoreCase() Method
  • Compare two strings using == operator
  • Using a custom method that compares two strings
  • Avoiding java.lang.NullPointerException

Compare strings values using String.equals()

This is the best way to check if two strings are equal. The equals method checks if 2 strings objects have the same value, then returns true or false.

How does it work in practice?

    String fruit1 = "kiwi";
    String fruit2 = "papaya";
    String fruit3 = new String("mango");
    String tropicalFruit1 = fruit1;
    String tropicalFruit2 =  "papaya";
    String tropicalFruit3 = new String("mango");


    System.out.println(fruit1.equals(fruit2));
    System.out.println(fruit1.equals(tropicalFruit1));
    System.out.println(fruit2.equals(tropicalFruit2));
    System.out.println(fruit3.equals(tropicalFruit3));

Output:
false
true
true
true

 

Compare strings Alphabetically using String.equalsIgnoreCase()

The only difference of this method comparing to equals is that equalsIgnoreCase method is not case sensitive.
equalsIgnoreCase checks if 2 strings objects have the same value without taking into consideration the case (lower or upper), then returns true or false.
How does it work in practice?

                String fruit1 = "Kiwi";
    String fruit2 = "Papaya";
    String fruit3 = new String("Mango");
    String tropicalFruit1 = fruit1;
    String tropicalFruit2 =  "papaya";
    String tropicalFruit3 = new String("mango");


    System.out.println(fruit2.equals(tropicalFruit2));
    System.out.println(fruit2.equalsIgnoreCase(tropicalFruit2));
    System.out.println(fruit3.equalsIgnoreCase(tropicalFruit3));

Output:
false
true
true

 

Java String comparison using String.compareTo() Method

compareTo() method provides a more precise comparison. In addition to checking if two strings are equal or not, it also allows determining which string is alphabetically greater.
if (string1 > string2) it returns a positive value.
if both the strings are equal lexicographically
i.e.(string1 == string2) it returns 0.
if (string1 < string2) it returns a negative value.
How does it work in practice?

    String fruit1 = "kiwi";
    String fruit2 = "papaya";
    String fruit3 = new String("mango");
    String tropicalFruit1 = fruit1;
    String tropicalFruit2 =  "papaya";
    String tropicalFruit3 =new String("mango");


    System.out.println(fruit1.compareTo(fruit2));
    System.out.println(fruit2.compareTo(fruit3));
    System.out.println(fruit2.compareTo(tropicalFruit2));
    System.out.println(fruit3.compareTo(tropicalFruit3));

Output:
-5
3
0
0

 

Java String comparison using String.compareToIgnoreCase() Method

As you might guess, it’s the same difference between String.equals() and String.equalsIgnoreCase()

Compare two strings using == operator

 

== operators for reference comparison, not a content comparison.
In other words, == checks if both objects point to the same memory location and do not perform a comparison of values in the objects.

    String fruit1 = "kiwi";
    String fruit2 = "papaya";
    String fruit3 = new String("mango");
    String tropicalFruit1 = fruit1;
    String tropicalFruit2 =  "papaya";
    String tropicalFruit3 = new String("mango");


    System.out.println(fruit1 == fruit2);
    System.out.println(fruit1 == tropicalFruit1);
    System.out.println(fruit2 == tropicalFruit2);
    System.out.println(fruit3 == tropicalFruit3);

Output:
false
true
true
false

 

If your goal is to check if a String object is null, then == operator allows you to do this verification, and it’s better than using equals().

if(fruit1 != null){ 
        System.out.println("The fruit "+ fruit1 +" is available" ); 
}

 

Using a custom method that compares two strings

Why would you create a custom method to compare two strings?
If you have specific rules to compare two strings, then the only solution is to create your own comparison method and implement these rules.

Avoiding java.lang.NullPointerException

In order to avoid java.lang.NullPointerException, you should call equals() method on a String object, which is either literal or not null.

Example

String tropicalFruit1 = null; 
// This code will break because tropicalFruit1 is null 
//java.lang.NullPointerException 
System.out.println(tropicalFruit1.equals("kiwi")); 
// this returns the comparison result : false 
System.out.println("kiwi".equals(tropicalFruit1));

 

Recent Posts