Java String comparison

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 references of the compared strings are equal, otherwise returns false.

package com.w3schools;
class TestString{
    String str1 = "w3schools";
    String str2 = "w3schools";
    String str3 = new String("w3schools");
    
    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 the string class overrides it. String class provides the following format of equals() method.

 

  • public boolean equals(Object obj): Compares the string with the specified object. Returns true if the actual content is equal, otherwise returns false.
  • public Boolean equalsIgnoreCase(String str): Compares the actual content of the string with the content of the specified string. Returns true if two string’s content is equal ignoring case, otherwise returns false.
package com.w3schools;
class TestString{
    String str1 = "w3schools";
    String str2 = "w3schools";
    String str3 = new String("w3schools");
    String str4 = "Roy";
    String str5 = "W3SCHOOLS360";
    
    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.

  •   public int compareTo(String str)
  •   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.

package com.w3schools;
class TestString{
    String str1 = "w3schools";
    String str2 = "w3schools";
    String str3 = "roy";
    String str4 = "alex";
    
    public void stringComparison(){
        System.out.println(str1.compareTo(str2));
        System.out.println(str1.compareTo(str3));
        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
5
17