Java String Comparison: A Comprehensive Guide
Table of Content:
The String class includes several methods that compare strings or substrings within strings. Each is examined here.
There are three ways to compare string in java:
- By equals() method
- By = = operator
- By compareTo() method
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:
Methods | Description |
public boolean equals(Object another) | compares this string to the specified object. |
public boolean equalsIgnoreCase(String another) | compares this String to another string, ignoring case. |
Program: equals
class Stringcomparison{ public static void main(String args[]){ String s1="Rahim"; String s2="Rahim"; String s3=new String("Rahim"); String s4="Ram"; 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 Press any key to continue . . .
Program: equalsIgnoreCase
class Teststringcomparison{ public static void main(String args[]){ String s1="RAMBO"; String s2="rambo"; System.out.println(s1.equals(s2));//false System.out.println(s1.equalsIgnoreCase(s2));//true } }
Output
false true Press any key to continue . . .
More examples Program
Following example compares two strings by using strcompareTo(string)
, str compareToIgnoreCase(String)
and str compareTo(object string)
of string class and returns the ascii difference of first odd characters
of compared strings.
Program
public class StringCompare{ public static void main(String args[]){ String str = "Hello Rambo"; String anotherString = "hello rambo"; Object objStr = str; System.out.println( str.compareTo(anotherString) ); System.out.println( str.compareToIgnoreCase(anotherString) ); System.out.println( str.compareTo(objStr.toString())); } }
Output
The above code sample will produce the following result.
-32 0 0 Press any key to continue . . .
2) String compare by == operator
This operators that can be used with object references are comparing for equality (==). These operators compare two values to see if they refer to the same object. Although this comparison is very fast,
Program
class Stringcomparison { public static void main(String args[]){ String s1="Atnyla"; String s2="Atnyla"; String s3=new String("Atnyla"); 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
The above code sample will produce the following result.
true false Press any key to continue . . .
3) String compare by compareTo() method
The String compareTo() method compares values lexicographically and returns an integer value that describes if first string is less than, equal to or greater than second string.
Suppose s1 and s2 are two string variables. If:
- stng1 == stng2 :0
- stng1 > stng2 :positive value
- stngs1 < stng2 :negative value
Program
class Stringcomparison { public static void main(String args[]){ String stng1="Atnyla"; String stng2="Atnyla"; String stng3="Rambo"; System.out.println(stng1.compareTo(stng2));//0 System.out.println(stng1.compareTo(stng3));//1(because stng1>s3) System.out.println(stng3.compareTo(stng1));//-1(because stng3 < s1 ) } }
Output
0 -17 17 Press any key to continue . . .
Equality comparison: One way for primitives, Four ways for objects
Comparison | Primitives | Objects |
---|---|---|
a == b , a != b |
Equal values | Compares references, not values. The use of == with object references is generally limited to the following:
|
a.equals(b) |
N/A | Compares values for equality. Because this method is defined in the Object class, from which all other classes are derived, it's automatically defined for every class. However, it doesn't perform an intelligent comparison for most classes unless the class overrides it. It has been defined in a meaningful way for most Java core classes. If it's not defined for a (user) class, it behaves the same as ==.
It turns out that defining |
a.compareTo(b) |
N/A | Comparable interface. Compares values and returns an int which tells if the values compare less than, equal, or greater than. If your class objects have a natural order, implement the Comparable |
compare(a, b) |
N/A | Comparator interface. Compares values of two objects. This is implemented as part of the Comparatorsort() or for use by sorting data structures such as TreeMap and TreeSet. You might want to create a Comparator object for the following.
If your class objects have one natural sorting order, you may not need this. |