Java Character equals() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Character

Definition

In Java, a.equals(obj) returns true when obj is also a Character and holds the same char value. Any other type, such as an Integer or a String, gives false, even if it looks like the same thing.

Use equals, not ==, to compare two Character objects. == on objects checks whether both variables point to the same object, which is only reliable for small cached values.

An instance method of java.lang.Character (Java 1.0+): call it on a Character object, as in boxed.equals(...), not on the class name. No import is needed, because java.lang is always available.

Syntax

Call it on a Character object and pass any object.

Java
Character ch = 'a';
ch.equals('a');          // true, 'a' is autoboxed to Character
ch.equals((char) 97);    // true, same char value
ch.equals(97);           // false, 97 is boxed to Integer
ch.equals("a");          // false, a String is not a Character
ch.equals(null);         // false
CallWhat it does
a.equals(b)Same char value, both Character.
list.contains('x')Collections call equals for you.
a.charValue() == b.charValue()Compare the primitive values directly.

One overload, equals(Object obj), since Java 1.0. It overrides Object.equals.

Library declarations
Java
// java.lang.Character (since Java 1.0)
public boolean equals(Object obj)

Parameters

ParameterDescription
obj (Object)The object to compare with; may be null.

Return Value

Return ValueDescription
trueobj is a Character with the same value.
falseobj is null, another type, or a different char.

Compare with Similar Methods

Ways to compare characters:

MethodHow it differs
a.compareTo(b)Also tells you which one is larger.
a.hashCode()Equal Characters always have equal hash codes.
String.equalsIgnoreCase()Case-insensitive comparison for strings.

Java Examples

Example 1 — Compare Character Objects with equals

Compare two Characters with the same value, one with a different value, and null.

Java
public class CharacterEqualsBasic {
    public static void main(String[] args) {
        Character a = 'x';
        Character b = Character.valueOf('x');
        Character c = 'y';
        System.out.println("a.equals(b):    " + a.equals(b));
        System.out.println("a.equals(c):    " + a.equals(c));
        System.out.println("a.equals(null): " + a.equals(null));
    }
}

How It Works

  1. Same value. a was autoboxed and b came from valueOf, but both hold 'x', so equals is true.
  2. Different value. 'x' and 'y' are different chars, so the result is false.
  3. null is safe as an argument. equals(null) simply returns false; only calling it on a null variable would fail.

Example 2 — Why ch.equals(97) Is false

Pass an int, a char, and a String to equals, then compare with == on the primitive.

Java
public class CharacterEqualsTypes {
    public static void main(String[] args) {
        Character ch = 'a';
        System.out.println("equals(97):        " + ch.equals(97));
        System.out.println("equals((char) 97): " + ch.equals((char) 97));
        System.out.println("equals(\"a\"):       " + ch.equals("a"));
        System.out.println("ch == 97:          " + (ch == 97));
    }
}

How It Works

  1. int becomes Integer. The parameter type is Object, so 97 is boxed to an Integer, which is never equal to a Character.
  2. Cast to char. (char) 97 is boxed to a Character holding 'a', so this time the answer is true.
  3. == compares numbers. In ch == 97, Java unboxes ch to the number 97, so the primitive comparison is true; the String "a" is another type and fails equals.

Example 3 — Search a List of Characters

contains and indexOf call equals on each element. Count matches yourself too.

Java
import java.util.ArrayList;

public class CharacterEqualsCollections {
    public static void main(String[] args) {
        ArrayList<Character> votes = new ArrayList<Character>();
        votes.add('y');
        votes.add('n');
        votes.add('y');
        votes.add('y');
        Character yes = 'y';
        int count = 0;
        for (Character v : votes) {
            if (yes.equals(v)) count++;
        }
        System.out.println("yes votes: " + count);
        System.out.println("contains n: " + votes.contains('n'));
        System.out.println("index of z: " + votes.indexOf('z'));
    }
}

How It Works

  1. Constant first. yes.equals(v) calls the method on a value that is never null, so a null element would just count as a mismatch.
  2. contains uses equals. 'n' is autoboxed and compared with each element until a match is found.
  3. No match. No element equals 'z', so indexOf returns -1.

Frequently Asked Questions

Only when the argument is a Character object holding the same char value.
97 is an int, which is boxed to Integer. An Integer is never equal to a Character, whatever its value.
No, it returns false. Calling equals on a null Character variable does throw NullPointerException.
Use equals. == compares object identity, which only works reliably for values cached by valueOf, \u0000 to \u007F.
Yes. 'a' and 'A' are different chars. Compare Character.toLowerCase of both values for a simple case-insensitive check.

Next: forDigit()

Turn a number from 0 to 35 into its digit character for a given radix.

forDigit() tutorial →

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.

7 people found this page helpful