C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Warning: With the equality operator == only the references are compared, not the exact characters in two strings.
So: This can give incorrect results when two strings contain the same chars, but are not the same reference.
Result: The three strings are all considered equal—equals() returns true. But "456" is not equal to the test strings.
Java program that uses String equals
public class Program {
public static void main(String[] args) {
String test = "1234";
String test2 = "1234";
String test3 = Integer.toString(1234);
if (test.equals(test2)) {
System.out.println(true);
}
if (test.equals(test3)) {
System.out.println(true);
}
if (!test.equals("456")) {
System.out.println(false);
}
}
}
Output
true
true
false
Here: The two strings contain the same chars, but are not the same instance. Equals() returns true, but the == operator returns false.
Java program that tests equality operator, equals
public class Program {
public static void main(String[] args) {
String test = "carrot1";
String test2 = "carrot" + Integer.toString(1);
// Test the equals, not equals operator.
if (test != test2) {
System.out.println("Not equal");
System.out.println(test);
System.out.println(test2);
}
// The equals method is different.
if (test.equals(test2)) {
System.out.println("Equals = true");
System.out.println(test);
System.out.println(test2);
}
}
}
Output
Not equal
carrot1
carrot1
Equals = true
carrot1
carrot1
Result: The program has a string "APPLE" and a string "apple." EqualsIgnoreCase returns true when we test those two strings.
Java program that uses equalsIgnoreCase
public class Program {
public static void main(String[] args) {
String name = "APPLE";
String name2 = name.toLowerCase();
// The uppercase and lowercase letters are considered equal.
if (name.equalsIgnoreCase(name2)) {
System.out.println(true);
System.out.println(name);
System.out.println(name2);
}
}
}
Output
true
APPLE
apple
Java program that uses equals, null
public class Program {
public static void main(String[] args) {
String value = "radish";
String other = null;
// The null literal is not equal to any String.
if (!value.equals(other)) {
System.out.println(true);
}
}
}
Output
true
Argument: We pass the other object to the contentEquals method. For example we can pass another String or a StringBuilder.
StringBuilderResult: This returns true if the character data in both objects is equal. It returns false otherwise.
Tip: With contentEquals we can avoid converting some objects (like a StringBuilder) to a String for comparison—this improves performance.
Java program that uses contentEquals
public class Program {
public static void main(String[] args) {
// Create 2 strings with the same character data.
String part1 = "cat100";
String part2 = "cat";
part2 += String.valueOf(100);
// Create a StringBuilder with the same character data.
StringBuilder builder = new StringBuilder();
builder.append("cat");
builder.append(100);
// Use contentEquals to compare character data.
if (part1.contentEquals(part2)) {
System.out.println("True 1");
}
if (part1.contentEquals(builder)) {
System.out.println("True 2");
}
}
}
Output
True 1
True 2