In Java, the hash code of a Character is just its char value as an int: 'A' has hash code 65. Hash-based collections such as HashSet and HashMap use this number to decide where to store each element.
There are two forms. The instance method boxed.hashCode() works on a Character object; the static method Character.hashCode(c), added in Java 8, takes a primitive char and does not need an object.
hashCode exists as an instance method, boxed.hashCode() (Java 1.0), and as a static method, Character.hashCode(char) (Java 8). Both return the same number. No import is needed, because java.lang is always available.
Syntax
Call the instance form on an object, or the static form with a char.
Java
Character boxed = 'A';
int h1 = boxed.hashCode(); // 65
int h2 = Character.hashCode('A'); // 65, Java 8+
int h3 = (int) boxed.charValue(); // 65, the same number
Call
What it does
boxed.hashCode()
Hash of a Character object.
Character.hashCode(c)
Hash of a primitive char, without boxing (Java 8+).
31 * h + Character.hashCode(c)
Combine char hashes in your own hashCode method.
public int hashCode() since Java 1.0, and public static int hashCode(char value) since Java 8. On Java 7 and older, use (int) c for a primitive.
Library declarations
Java
// java.lang.Character
public int hashCode() // Java 1.0
public static int hashCode(char value) // Java 8
Parameters
Parameter
Description
(none)
Instance form: no parameters.
value (char)
Static form: the char to hash.
Return Value
Return Value
Description
int
The char value, from 0 to 65535; the same as charValue() widened to int.
Example 1 — Instance hashCode, Static hashCode, and the Char Value
Get the hash code of 'A' three ways.
Java
public class CharacterHashCodeBasic {
public static void main(String[] args) {
Character boxed = 'A';
System.out.println("instance: " + boxed.hashCode());
System.out.println("static: " + Character.hashCode('A'));
System.out.println("value: " + (int) boxed.charValue());
}
}
Output
instance: 65
static: 65
value: 65
How It Works
Instance form.boxed.hashCode() is called on the object and returns 65.
Static form, Java 8+.Character.hashCode('A') gives the same answer without creating a Character.
Nothing hidden. Both equal the char code itself, which the cast (int) boxed.charValue() shows directly.
Example 2 — Remove Duplicate Letters with a HashSet
A HashSet uses hashCode and equals to spot duplicates. Print each new letter with its hash code.
Java
import java.util.HashSet;
import java.util.Set;
public class CharacterHashCodeSet {
public static void main(String[] args) {
String word = "banana";
Set<Character> seen = new HashSet<Character>();
for (int i = 0; i < word.length(); i++) {
Character c = word.charAt(i);
if (seen.add(c)) {
System.out.println(c + " -> hash " + c.hashCode());
}
}
System.out.println("unique letters: " + seen.size());
System.out.println("contains 'n': " + seen.contains('n'));
}
}
Output
b -> hash 98
a -> hash 97
n -> hash 110
unique letters: 3
contains 'n': true
How It Works
add tells you if it is new.seen.add(c) returns true only the first time a letter appears, so each letter prints once.
The hash picks the bucket. The set uses 98, 97, and 110 to find where b, a, and n belong, then equals confirms a match.
Order of first appearance. The lines follow the order of the word, not the set's internal order, because they are printed inside the loop.
Example 3 — Check the equals and hashCode Contract
Equal objects must have equal hash codes. Verify it for two Character objects and for a custom hash that combines chars.
Java
public class CharacterHashCodeContract {
public static void main(String[] args) {
Character x = 'q';
Character y = Character.valueOf('q');
System.out.println("equal objects: " + x.equals(y));
System.out.println("equal hashes: " + (x.hashCode() == y.hashCode()));
int h = 0;
char[] code = {'o', 'k'};
for (char c : code) {
h = 31 * h + Character.hashCode(c);
}
System.out.println("combined hash: " + h + ", same as String: " + (h == "ok".hashCode()));
}
}
Output
equal objects: true
equal hashes: true
combined hash: 3548, same as String: true
How It Works
The rule. If x.equals(y) is true, their hash codes must match; for Character both are 113.
Combining hashes. The loop uses the common 31 * h + part formula: 31 * 111 + 107 = 3548.
Same as String.String.hashCode() uses that formula over its chars, so "ok".hashCode() is also 3548.
Frequently Asked Questions
Its char value as an int. Character.valueOf('A').hashCode() is 65.
In Java 8. Before that, use (int) c or Character.valueOf(c).hashCode().
No. Each char maps to its own value from 0 to 65535, so hash codes of different chars never collide.
The number is the same, but hashCode returns an int while charValue returns a char.
It uses the hash code to choose a bucket, then equals to check for an exact match inside that bucket.