In order to compare Strings for equality, you should use the String object’s equals or equalsIgnoreCase methods. We will also see why we should not use the == operator to compare strings. Show Comparing Strings with equals() MethodIf we need to compare two strings in java and also care about the casing of the strings we can use the equals() method. For example, the following snippet will determine if the two instances of String are equal on all characters including casing: public class CompareTwoStrings { public static void main(String[] args) { String firstString = "Test123"; String secondString = "Test" + 123; String thirdString = "TEST123"; if (firstString.equals(secondString)) { System.out.println("first and second strings are equal"); } if (firstString.equals(thirdString)) { System.out.println("first and third string are equal"); } } }Output: first and second strings are equalComparing Strings with equalsIgnoreCase() MethodIf we need to compare two strings in java but don’t care about the casing of the strings we can use the equalsIgnoreCase() method. For example, in the above code snippet, if we replaced .equals() with .equalsIgnoreCase() method, then both print statements get executed: public class CompareTwoStrings { public static void main(String[] args) { String firstString = "Test123"; String secondString = "Test" + 123; String thirdString = "TEST123"; if (firstString.equalsIgnoreCase(secondString)) { System.out.println("first and second strings are equal"); } if (firstString.equalsIgnoreCase(thirdString)) { System.out.println("first and third string are equal"); } } }Output: first and second strings are equal first and third string are equalDo not use the == operator to compare StringsNote: When comparing two strings in java, we should not use the == or != operators. These operators actually test references, and since multiple String objects can represent the same String, this is liable to give the wrong answer. Instead, use the String.equals(Object) method, which will compare the String objects based on their values. public class CompareTwoStrings { public static void main(String[] args) { String firstString = "Test123"; String secondString = "Test123"; String thirdString = new String("Test123"); if (firstString == secondString) { System.out.println("first and second strings are equal"); } if (firstString == thirdString) { System.out.println("first and third strings are equal"); } } }Output: first and second strings are equalComparing Strings With Constant ValuesWhen comparing a String to a constant value, you can put the constant value on the left side of equals to ensure that you won’t get a NullPointerException if the other String is null. For example: "baz".equals(foo)While foo.equals("baz") will throw a NullPointerException if foo is null, "baz".equals(foo) will evaluate to false. A more readable alternative is to use Objects.equals(), which does a null check on both parameters: e.g. Objects.equals(foo, "baz"). Comparing Strings in a Switch StatementAs of Java 1.7, it is possible to compare a String variable to literals in a switch statement. Make sure that the String is not null, otherwise it will always throw a NullPointerException. Values are compared using String.equals, i.e. case sensitive. public class CompareTwoStrings { public static void main(String[] args) { String stringToSwitch = "A"; switch (stringToSwitch) { case "a": System.out.println("a"); break; case "A": System.out.println("A"); //the code goes here break; case "B": System.out.println("B"); break; default: break; } } }ConclusionIn this post we explained how to compare strings in java with code examples. When casing of the strings matters, we should use .equals() and when casing is not important, then we should use .equalsIgnoreCase(). Moreover, we should not use the == operator to compare strings, as the == operator checks the reference and not the value.
View Discussion Improve Article Save Article Like Article
View Discussion Improve Article Save Article Like Article
Both equals() method and the == operator are used to compare two objects in Java. == is an operator and equals() is method. But == operator compares reference or memory location of objects in a heap, whether they point to the same location or not. In general, both equals() and “==” operators in Java are used to compare objects to check equality, but here are some of the differences between the two:
Output true false true true Explanation: Here, we create two objects, namely s1 and s2.
Java String Pool
Java Heap Let us understand both the operators in detail: Equality operator(==)We can apply equality operators for every primitive type, including the boolean type. We can also apply equality operators for object types.
Output false false true true If we apply == for object types then, there should be compatibility between arguments types (either child to parent or parent to child or same type). Otherwise, we will get a compile-time error.
Output: false false // error: incomparable types: Thread and String.equals() MethodIn Java, the String equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are the same, it returns true. If all characters are not matched, then it returns false.
Output true false false false true Explanation: Here, we are using the .equals method to check whether two objects contain the same data or not.
This article is contributed by Veturi Lakshmi Prathyusha. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. |