Java String compare

compareTo() method is used to perform natural sorting on string. Natural sorting means the sort order which applies on the object, e.g., lexical order for String, numeric order for Sorting integers

Lexical order is nothing but alphabetically order. compareTo methods does a sequential comparison of letters in the string that have the same position.

There are three ways to compare string in java:
  1. By equals() method
  2. By = = operator
  3. By compareTo() method
Difference between == and .equals() method in Java

In general both equals() and “==” operator in Java are used to compare objects to check equality but here are some of the differences between the two:

  1. Main difference between .equals() method and == operator is that one is method and other is operator.

  2. We can use == operators for reference comparison (address comparison) and .equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.

  3. If a class does not override the equals method, then by default it uses equals(Object o) method of the closest parent class that has overridden this method. See this for detail

  4. Coding Example:

// Java program to understand // the concept of == operator public class Test { public static void main(String[] args) { String s1 = new String("HELLO"); String s2 = new String("HELLO"); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); } }


Output:
false true
1) String compare by equals() method

The String equals() method compares the original content of the string. It compares values of string for equality. String class provides two methods:

  • public boolean equals(Object another) compares this string to the specified object.

  • public boolean equalsIgnoreCase(String another) compares this String to another string, ignoring cas

class Teststringcomparison1{ public static void main(String args[]){ String s1="Ravi"; String s2="Ravi"; String s3=new String("Ravi"); String s4="Kumar"; System.out.println(s1.equals(s2));//true System.out.println(s1.equals(s3));//true System.out.println(s1.equals(s4));//false } }


Output:
true true false
class Teststring{ public static void main(String args[]){ String s1="Rama"; String s2="RAMA"; System.out.println(s1.equals(s2));//false System.out.println(s1.equalsIgnoreCase(s2));//true } }


Output:
false true
) String compare by == operator
class Teststringcomparison3{ public static void main(String args[]){ String s1="Rama"; String s2="Rama"; String s3=new String("Rama"); System.out.println(s1==s2);//true (because both refer to same instance) System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool) } }


Output:
true false
3) String compare by compareTo() method

This Java method returns an int datatype which is based on the lexicographical comparison between two strings.

  • returns < 0 then the String calling the method is lexicographically first

  • returns == 0 then the two strings are lexicographically equivalent

  • returns > 0 then the parameter passed to the compareTo method is lexicographically first.

Suppose s1 and s2 are two string variables. If: s1 == s2 :0 s1 > s2 :positive value s1 < s2 :negative value
public class Sample_String { public static void main(String[] args) { String str_Sample = "a"; System.out.println("Compare To 'a' b is : " + str_Sample.compareTo("b")); str_Sample = "b"; System.out.println("Compare To 'b' a is : " + str_Sample.compareTo("a")); str_Sample = "b"; System.out.println("Compare To 'b' b is : " + str_Sample.compareTo("b")); } }


Output:
Compare To 'a' b is : -1 Compare To 'b' a is : 1 Compare To 'b' b is : 0
class Teststring{ public static void main(String args[]){ String s1="Rama"; String s2="Rama"; String s3="Pavi"; System.out.println(s1.compareTo(s2));//0 System.out.println(s1.compareTo(s3));//2(because s1>s3) System.out.println(s3.compareTo(s1));//-2(because s3 < s1 ) } }


Output:
0 2 -2



Instagram