How to compare two strings in Java

In this tutorial, we will learn how to compare two strings in Java.
There are different ways to do that –
1. By using String.equals() method
2. By using compareTo()  method
3. By using == operator
4. By using iterative method

Using equals() method compare two strings in Java

This is the simplest way to find out if two strings are equal in Java. This method compares two strings and returns true if strings are equal or returns false if the strings aren’t equal.

The following program shows the implementation:

package part_a;

public class Example {

  public static void main(String[] args) {
    
    //Initialize strings
    String one = "Classmate";
    String two = "Classmate";
    String three = "Neighbor";
    
    //Compare the strings with each other
    System.out.println(one.equals(two));
    System.out.println(one.equals(three));
    System.out.println(two.equals(three));
    
  }

}

Output – 

true
false 
false

There is another version of string.equals() which ignores the case and jut compares both the strings lexicographically. The method is equalsIgnoreCase(). This method is helpful when you just want to compare characters irrespective of the case.
The following is a program of the same –

public class Example {

  public static void main(String[] args) {
    
    //Initialize strings
    String one = "Classmate";
    String two = "CLASSMATE";
    
    //Compare the two strings
    System.out.println(one.equals(two));
    System.out.println(one.equalsIgnoreCase(two));
    
    
  }

}

Output – 

false
true

compareTo() method to compare two strings in Java

This method compares two strings lexicographically and returns an integer which tells us if the string is equal, less than, or greater than the second string.
Scenarios –

  • 0 is returned if String1 == String 2
  • positive value is returned if String1 > String2
  • negative value is returned if String1 > String2
public class Example {

  public static void main(String[] args) {
    
    //Initialize strings
    String one = "Classmate";
    String two = "Classmate";
    String three = "Neighbor";
    String four = "zedd";
    
    //Compare the strings with each other
    System.out.println(one.compareTo(two));
    System.out.println(one.compareTo(three));
    System.out.println(four.compareTo(three));
    System.out.println(four.compareTo(one));
    
  }

}

Output – 

0 //Because the strings are equal
-11 //Because 'N' has a higher ASCII value than 'C'
44 // Because 'z' has a higher ASCII value than 'N' 
55 // Because 'z' has a higher ASCII value than 'C'

Using == operator

We should be careful while using this operator because this operator compares the references of the two strings and not the two string themselves. To give you an idea, this is an example –

public class Example {

  public static void main(String[] args) {
    
    //Initialize strings
    String one = "Classmate";
    String two = "Classmate";
    String three = new String("Classmate");
    
    //Compare the strings with each other
    System.out.println(one == two);
    System.out.println(one == three);
    System.out.println(two == three);
    
  }

}

Output – 

true
false
false

Using iterative method

The following is a simple java program which compares two strings character by character, using iterative method –

public class Example {

  public static void main(String[] args) {
    
    //Initialize strings
    String one = "Classmate";
    String two = "Classmate";
    String three = "Neighbor";
    
    //A flag variable
    boolean flag = true;
    
    //Compare the strings with each other
    for(int i=0; (i<one.length()||i<two.length()); i++) 
    {
      if(one.length()!=two.length())
      {
        flag=false;
        break;
      }
      if(one.charAt(i) == two.charAt(i))
        flag=true;
      else
        flag=false;
    }
    System.out.println(flag);
    
    for(int i=0; (i<one.length()||i<three.length()); i++) 
    {
      if(one.length()!=three.length())
      {
        flag=false;
        break;
      }
      if(one.charAt(i) == three.charAt(i))
        flag=true;
      else
        flag=false;
    }
    System.out.println(flag);
    
    for(int i=0; (i<two.length()||i<three.length()); i++) 
    {
      if(two.length()!=three.length())
      {
        flag=false;
        break;
      }
      if(three.charAt(i) == two.charAt(i))
        flag=true;
      else
        flag=false;
    }
    System.out.println(flag);

  }

}

Output – 

true
false
false

Related articles –

Leave a Reply

Your email address will not be published. Required fields are marked *