Java String equalsIgnoreCase() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, equalsIgnoreCase() checks whether two strings have the same letters when case is ignored. It returns true or false.

You pass the other string. It is required. Whitespace still matters. For sort order ignoring case, use compareToIgnoreCase().

Syntax

Call equalsIgnoreCase on the string. Start with this line.

java
boolean ok = a.equalsIgnoreCase(b);
CallWhat it does
a.equalsIgnoreCase(b)Returns true when a and b match ignoring letter case.

This is for equality, not sorting. Prefer compareToIgnoreCase() when you need order.

Library declarations
java
public boolean equalsIgnoreCase(String anotherString);

Parameters

ParameterDescription
anotherStringRequired. The string to compare with this instance.

Return Value

Return ValueDescription
trueThe strings match when case is ignored.
falseThe strings do not match when case is ignored.

Java Examples

Example 1 — Basic equalsIgnoreCase

Confirm two strings match when only capitalization differs.

java
public class EqualsIgnoreCaseBasic {
    public static void main(String[] args) {
        String a = "Java";
        String b = "java";

        boolean ok = a.equalsIgnoreCase(b);

        System.out.println("equalsIgnoreCase = " + ok);
    }
}

How It Works

  1. Store both strings. a and b differ only in the first letter’s case.
  2. Call equalsIgnoreCase. Java compares after treating case as equivalent.
  3. Get true. Every position matches when case is ignored.
  4. Print the boolean. Useful for quick checks in demos and tests.

Example 2 — equals vs equalsIgnoreCase

Side-by-side with equals() so the case rule is obvious.

java
public class EqualsVsIgnoreCase {
    public static void main(String[] args) {
        String left = "YES";
        String right = "yes";

        System.out.println("equals          = " + left.equals(right));
        System.out.println("equalsIgnoreCase = " + left.equalsIgnoreCase(right));
    }
}

How It Works

  1. Pick two spellings. "YES" and "yes" are the same word in different case.
  2. Try equals. Exact code-unit match fails → false.
  3. Try equalsIgnoreCase. Case is ignored → true.
  4. Choose by intent. Passwords usually need equals; commands often want ignore-case.

Example 3 — Accept y / Y / yes

Put the literal on the left so a null answer cannot cause NullPointerException. Prefer this over manual toLowerCase() for simple checks.

java
public class EqualsIgnoreCaseInput {
    public static boolean isYes(String answer) {
        return "y".equalsIgnoreCase(answer)
            || "yes".equalsIgnoreCase(answer);
    }

    public static void main(String[] args) {
        System.out.println(isYes("Y"));
        System.out.println(isYes("Yes"));
        System.out.println(isYes("no"));
        System.out.println(isYes(null));
    }
}

How It Works

  1. Literal on the left. "y".equalsIgnoreCase(answer) is safe if answer is null.
  2. Allow short or long yes. Match "y" or "yes" in any case.
  3. Reject other text. "no" matches neither → false.
  4. Null returns false. No exception—handy for optional input.

Frequently Asked Questions

equalsIgnoreCase() checks whether two strings match while ignoring letter case.
equals() is case-sensitive. equalsIgnoreCase() ignores case.
anotherString is required.
Yes. Spaces and tabs still have to match.
No. Use compareToIgnoreCase() when you need order.

Next: format()

Learn how to build formatted strings with placeholders.

format() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful