- Instant help with your Java coding problems

Compare strings in Java

Question:
How to compare strings in Java?
Answer:
String text1 = "Hello";
String text2 = "World";

if (text1.equals(text2)) {
    System.out.println("Equals"); 
} else {
    System.out.println("Doesn't equal"); 
}
Description:

In Java, you can not use the == or === operators to compare strings. Instead, use the equals() method.

equals() compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Reference:
equals() manual
Share "How to compare strings in Java?"
Related snippets:
Tags:
java, string, compare, equal
Technical term:
Compare strings in Java