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.
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.
equals()
returns true. But "456" is not equal to the test strings.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
operatorThe == 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.
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.
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
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.
contentEquals
method. For example we can pass another String
or a StringBuilder
.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
.