«Back to Home

Core Java

Topics

Compare String In Java

Compare String

On the basis of content and reference, we can compare the string in Java.
 
Three ways to compare the string in Java are,
  1. String compared by equals() method

    The string in equals() method compares the original content of the string. It is mainly used to compare values of the string for the equality.

    String class provides two methods, which are,

    • public boolean equals(Object another)

      This method is used to compare the string to the specified object.

    • public boolean equalsIgnoreCase(String another)

      This method is used to compare the string to another string by ignoring the case.

      Let’s see an example, given below.

      Code
      1. public class CompareString {  
      2.     public static void main(String args[]) {  
      3.         String n1 = "Emily";  
      4.         String n2 = "Emily";  
      5.         String n3 = new String("Emily");  
      6.         String n4 = "Emilly";  
      7.         System.out.println(n1.equals(n2));  
      8.         System.out.println(n1.equals(n3));  
      9.         System.out.println(n1.equals(n4));  
      10.     }  
      11. }  
      16

      Output

      17

      Let’s see another example, given below.

      Code
      1. public class CompareString {  
      2.     public static void main(String args[]) {  
      3.         String n1 = "Peter";  
      4.         String n2 = "PETER";  
      5.         System.out.println(n1.equals(n2));  
      6.         System.out.println(n1.equalsIgnoreCase(n2));  
      7.     }  
      8. }  
      18

      Output

      19

  2. String compare by == operator

    When the string is compared by = = operator, it compares the references only and not the values.

    Let’s see an example, given below.

    Code
    1. public class CompareString {  
    2.     public static void main(String args[]) {  
    3.         String n1 = "Sophia";  
    4.         String n2 = "Sophia";  
    5.         String n3 = new String("Sophia");  
    6.         String n4 = new String("Sophia");  
    7.         String n5 = new String("Sophie");  
    8.         System.out.println(n1 == n2);  
    9.         System.out.println(n2 == n3);  
    10.         System.out.println(n3 == n4);  
    11.         System.out.println(n4 == n5);  
    12.     }  
    13. }  
    20

    Output

    21

  3. String compare by compareTo() method

    When the string is compared by compareTo() method, it compares the values and returns an integer value, which describes when first string is less than, equal to or greater than second string, the output is retrieved accordingly.

    Suppose n1 and n2 are two string variables and if

    • n1 == n2; it will return 0.

    • n1 > n2; it will return a positive value.

    • n1 < n2; it will return a negative value.

      Let’s see an example, given below.

      Code
      1. public class CompareString {  
      2.     public static void main(String args[]) {  
      3.         String n1 = "James";  
      4.         String n2 = "James";  
      5.         String n3 = "John";  
      6.         System.out.println(n1.compareTo(n2));  
      7.         System.out.println(n2.compareTo(n3));  
      8.         System.out.println(n3.compareTo(n1));  
      9.     }  
      10. }  
      22

      Output

      23
Summary

Thus, we learnt that on the basis of content and reference, we can compare the string in Java and also learnt their ways to compare the string in Java.