Library contents

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

  1. Start with "a" and "a"
  2. Do this Compare the whole text
  3. Result Equal
Both inputs contain the same letter in the same case.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println("a".equals("a"));
    }
}
Output
true

Syntax

Java 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.java

Accept 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.

From the programString expected = "BOX7"
First output linefalse
Main.java
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));
    }
}
Output
false
true

Only the letters differ in case. The exact comparison fails and the case-insensitive one succeeds.

Advertisements

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.

Resulttrue
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String scan = new String(new char[]{'A', '7'});
        System.out.println("A7".equals(scan));
    }
}
Output
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.

From the programString catalog = "sku-A"
Resultfalse
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String catalog = "sku-A";
        System.out.println(catalog.equals("SKU-A"));
    }
}
Output
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.

From the programString status = "Paid"
Resulttrue
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String status = "Paid";
        System.out.println(status.equalsIgnoreCase("PAID"));
    }
}
Output
true

The dashboard accepts either capitalization.

Advertisements

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.

From the programString scan = "a-12"
Resultfalse
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String scan = "a-12";
        System.out.println(scan.equalsIgnoreCase("A-21"));
    }
}
Output
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.

Documentation for String.equals and equalsIgnoreCase