Java String.equals and equalsIgnoreCase
Use equals to compare text exactly. Use equalsIgnoreCase when the rules allow either uppercase or lowercase letters.
A simple example
String.equals and equalsIgnoreCase
- Start with "a" and "a"
- Do this Compare the whole text
- Result Equal
class Main {
public static void main(String[] args) throws Exception {
System.out.println("a".equals("a"));
}
}
true
Syntax
expected.equals(entered)
expected.equalsIgnoreCase(entered)
JDK 21 method signatures (2)
public boolean equals(Object anObject)
public boolean equalsIgnoreCase(String anotherString)Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaAccept the same pickup code in lowercase
People enter short codes with different keyboard settings and capitalization. Match the input under the intended case rule so a valid code can be recognized without changing the stored identifier.
public class Main {
public static void main(String[] args) throws Exception {
String expected = "BOX7";
String entered = "box7";
System.out.println(expected.equals(entered));
System.out.println(expected.equalsIgnoreCase(entered));
}
}
false true
Only the letters differ in case. The exact comparison fails and the case-insensitive one succeeds.
Match a scanned ticket exactly
A reader compares two identifiers that may look similar on their labels. Use the full values under the intended case rules so a difference in one letter or digit is not lost.
class Main {
public static void main(String[] args) throws Exception {
String scan = new String(new char[]{'A', '7'});
System.out.println("A7".equals(scan));
}
}
true
The strings hold the same text even though they were created differently.
Keep case-sensitive product keys distinct
A product database uses letter case as part of its identifiers. Verify whether two labels that differ only in case should occupy distinct keys before aggregating their quantities.
class Main {
public static void main(String[] args) throws Exception {
String catalog = "sku-A";
System.out.println(catalog.equals("SKU-A"));
}
}
false
Changing the case changes this catalog identifier.
Recognize a delivery status
An intake queue uses a recognizable prefix or status to route incoming work. Check the required position and comparison rules so a matching fragment in an unrelated field cannot send a request to the wrong desk.
class Main {
public static void main(String[] args) throws Exception {
String status = "Paid";
System.out.println(status.equalsIgnoreCase("PAID"));
}
}
true
The dashboard accepts either capitalization.
Check whether two room codes differ
A timetable assigns rooms using short identifiers. Compare the complete codes before treating two bookings as belonging to the same room, including any character near the end that differs.
class Main {
public static void main(String[] args) throws Exception {
String scan = "a-12";
System.out.println(scan.equalsIgnoreCase("A-21"));
}
}
false
Ignoring letter case still preserves the order of the digits.
What to watch for
Use equals for case-sensitive codes. The == operator compares references when both operands are String objects.