Equals, equalsIgnoreCase. Java strings contain characters. These can be letters (lowercase and uppercase), digits, spaces. We want to compare 2 strings for equality.
With equals, we test the chars in 2 string objects. If all the characters are the same, equals() returns true. With equalsIgnoreCase, lowercase and uppercase are considered the same.
First example. Let us use equals() in a simple program. We introduce three test strings. They all contain the same characters, but the third one is not a string literal.
Result The three strings are all considered equal—equals() returns true. But "456" is not equal to the test strings.
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.
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);
}
}
}true
true
false
Equals operator. The == and != operators can be used on string references. But this does not handle the internal characters in the String objects. It just tests the references.
Here The two strings contain the same chars, but are not the same instance. Equals() returns true, but the == operator returns false.
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);
}
}
}Not equal
carrot1
carrot1
Equals = true
carrot1
carrot1
EqualsIgnoreCase. Sometimes we do not care if a letter is uppercase or lower. The EqualsIgnoreCase method treats both cases as equal. It does not care about upper and lower.
Result The program has a string "APPLE" and a string "apple." EqualsIgnoreCase returns true when we test those two strings.
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);
}
}
}true
APPLE
apple
Null. This program shows that a non-null string never equals a null string. Equals() will always return false when passed a null variable. But it throws no exception.
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);
}
}
}true
ContentEquals. This method tests the value of objects through their CharSequences. So it can compare a String against a StringBuilder. It is called on a String instance.
Detail We pass the other object to the contentEquals method. For example we can pass another String or a StringBuilder.
Result 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.
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");
}
}
}True 1
True 2
With Strings, we must keep in mind the difference between references and objects. A string variable is a reference, but this reference points to an object.
With equality operators (like "==") we can test two references for equality. But to check the data within String objects, we must use equals or equalsIgnoreCase.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 31, 2021 (edit).