Java Character hashCode() Method

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

Definition

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
CallWhat 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

ParameterDescription
(none)Instance form: no parameters.
value (char)Static form: the char to hash.

Return Value

Return ValueDescription
intThe char value, from 0 to 65535; the same as charValue() widened to int.

Compare with Similar Methods

Related methods:

MethodHow it differs
a.equals(b)Equal Characters must, and do, have equal hash codes.
boxed.charValue()Gives the same number once widened to int.
"text".hashCode()A String's hash combines all its char values.

Java Examples

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());
    }
}

How It Works

  1. Instance form. boxed.hashCode() is called on the object and returns 65.
  2. Static form, Java 8+. Character.hashCode('A') gives the same answer without creating a Character.
  3. 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'));
    }
}

How It Works

  1. add tells you if it is new. seen.add(c) returns true only the first time a letter appears, so each letter prints once.
  2. 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.
  3. 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()));
    }
}

How It Works

  1. The rule. If x.equals(y) is true, their hash codes must match; for Character both are 113.
  2. Combining hashes. The loop uses the common 31 * h + part formula: 31 * 111 + 107 = 3548.
  3. 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.

Next: highSurrogate()

Get the high (leading) surrogate char of a supplementary code point (Java 7+).

highSurrogate() 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.

13 people found this page helpful