How to compare two String in Java - String Comparison Example (original) (raw)
String comparison is a common programming task and Java provides several way to compare two String in Java. String is a special class in Java, String is immutable and It’s used a lot in every single Java program starting from simple test to enterprise Java application. In this Java String compare tutorial we will see different ways to compare two String in Java and find out how they compare String and when to use equals() or compareTo() for comparison etc.
Here are four examples of comparing String in Java
String comparison using equals method
String comparison using equalsIgnoreCase method
String comparison using compareTo method
String comparison using compareToIgnoreCase method
We will see String compare Example using each of this method in example section, before that let's get some theory:
Compare two String using equals method in Java
equals()method compare two Strings for content equality. So if two string contains the same letters, in the same order and in some case they will be equals by equals() method. equals() method is defined in Object class and String class overrides that for character-based comparison.
You can also check 5 tips to override equals()method in Java for more details on equals and its implementation. For example for two Strings "Sony" and "Sony", equals() will return true but for "Sony" and "SONY" it will return false.
Compare String using the equalsIgnoreCase method in Java
equalsIgnoreCase is more liberal than equals and compares two strings ignoring their case. So if two String contains the same characters and in the same order despite there case e.g. lower case, upper case, Capital case, or Camel case they will be equal by equalsIgnoreCase.
For example "sony" and "SONY" two Strings will be the same by equalsIgnoreCase and it will return true but "Sony" and "Samsung" will not be the same and it will return false because they don't contain same characters.
Comparing String using compareTo
compareTo is actual comparison method unlike equals()and equalsIgnoreCase() and tell us whether two Strings are lexicographically equal, precedes or follows each other. if you want to sort Strings lexicographically compareTo() method is used.
This is also called the natural order of String It returns zero if two Strings are same, less than zero if calling string comes before argument string and greater than zero if calling string comes later than argument string as shown in the example below.
See things to remember while overriding compareTo method in Java for more detail on compareTo method.
Compare String using compareToIgnoreCase
Similar to compareTo() method with ignoring case like equalsIgnoreCase() and return the same values as returned by compareTo during String comparison.
Don't use "==" for String comparison
Many Java programmer makes the mistake of using "==" for string comparison. "==" just check if two reference variables are pointing two same objects in Java heap and since String is immutable in Java and maintained in String pool two String literal refer same String object which gives the sense that "==" can be used to compare string which is incorrect. always use equals() method for equality check and compare method for actual string comparison.
Another way of comparing String is by writing a custom Comparator in Java. write your comparison logic in compare() method and then you can use that logic to compare two strings.
public class StringComparisonExample {
public static void main(String args[]) {
String tv = "Bravia";
String television = "Bravia";
// String compare example using equals
if (tv.equals(television)) {
System.out.println("Both tv and television contains same letters and equal by equals method of String");
}
// String compare example in java using compareTo
if (tv.compareTo(television) == 0) {
System.out.println("Both tv and television are equal using compareTo method of String");
}
television = "BRAVIA";
// Java String comparison example using equalsIgnoreCase
if (tv.equalsIgnoreCase(television)) {
System.out.println("tv and television are equal by equalsIgnoreCase method of String");
}
// String comparison example in java using CompareToIgnoreCase
if (tv.compareToIgnoreCase(television) == 0) {
System.out.println("tv and television are same by compareToIgnoreCase of String");
}
String sony = "Sony";
String samsung = "Samsung";
// lexicographical comparison of String in Java with ComapreTo
if (sony.compareTo(samsung) > 0) {
System.out.println("Sony comes after Samsung in lexicographical order");
} else if (sony.compareTo(samsung) < 0) {
System.out.println("Sony comes before Samsung in lexicographical order");
}
}
}
Output:
Both tv and television contains same letters and equal by equals method of String
Both tv and television are equal using compareTo method of String
tv and television are equal by equalsIgnoreCase method of String
tv and television are the same as compareToIgnoreCase of String
Sony comes after Samsung in lexicographical order
That's all about how to compare two String objects in Java. As I said, there are multiple ways to compare String in Java like using equals(), == operator, compare(), and compareTo() and each has their own special way to implement String objects like == compare memory location of string object while equals() compare content of String.
These were some common examples of comparing string in Java. Avoid the common mistake of using equality operator “==” for comparing String instead of always use equals() method.
Few more Java tutorials on String you may like