In Java, a.equals(obj) returns true when obj is also a Characterand 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
Call
What 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)
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));
}
}
int becomes Integer. The parameter type is Object, so 97 is boxed to an Integer, which is never equal to a Character.
Cast to char.(char) 97 is boxed to a Character holding 'a', so this time the answer is true.
== 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'));
}
}
Output
yes votes: 3
contains n: true
index of z: -1
How It Works
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.
contains uses equals.'n' is autoboxed and compared with each element until a match is found.
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.