Java Character valueOf() Method

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

Definition

In Java, Character.valueOf(c) returns a Character object holding the char c. This is called boxing, and it is what the compiler uses when you write Character boxed = 'x'; (autoboxing, Java 5+).

valueOf keeps a cache: it always returns the same object for chars from \u0000 to \u007F (the ASCII range). Other values may or may not be cached, so compare Character objects with equals, not ==.

A static method of java.lang.Character (Java 5+): call it on the class, as in Character.valueOf(...); you do not need a Character object. No import is needed, because java.lang is always available.

Syntax

Pass a primitive char.

Java
Character a = Character.valueOf('k');   // explicit boxing
Character b = 'k';                       // autoboxing: calls valueOf('k')
a == b                                   // true, 'k' is in the cache
a.equals(b)                              // true, always the right way
CallWhat it does
Character.valueOf(c)Box a char.
list.add(c)Autoboxing calls valueOf for you.
a.equals(b)Compare boxed values safely.

One overload, valueOf(char c), since Java 5. It is preferred over the Character(char) constructor, which is deprecated since Java 9.

Library declarations
Java
// java.lang.Character (since Java 5)
public static Character valueOf(char c)

Parameters

ParameterDescription
c (char)The char to wrap.

Return Value

Return ValueDescription
CharacterAn object holding c; the shared cached object when c is \u0000 to \u007F.

Compare with Similar Methods

Related conversions:

MethodHow it differs
boxed.charValue()The reverse: unwraps the char.
Character.toString(c)Makes a String instead of a Character.
a.equals(b)The right way to compare two boxed values.

Java Examples

Example 1 — Box a char Explicitly and Automatically

Create a Character with valueOf and with autoboxing, then use it.

Java
public class CharacterValueOfBasic {
    public static void main(String[] args) {
        Character explicit = Character.valueOf('k');
        Character auto = 'k';
        System.out.println("value: " + explicit);
        System.out.println("code: " + (int) explicit.charValue());
        System.out.println("equal: " + explicit.equals(auto));
    }
}

How It Works

  1. Two ways to box. Character.valueOf('k') is explicit; Character auto = 'k'; makes the compiler call valueOf for you.
  2. Printing an object. String concatenation calls toString() on the Character, so the letter k is printed.
  3. Same value. equals confirms both objects hold 'k', whose code is 107.

Example 2 — Cached Objects for ASCII, equals for Everything

Box 'A' twice and compare with ==, which is guaranteed for the cached range; for a char above \u007F, use equals.

Java
public class CharacterValueOfCache {
    public static void main(String[] args) {
        Character a1 = Character.valueOf('A');
        Character a2 = Character.valueOf('A');
        System.out.println("same object for 'A': " + (a1 == a2));
        Character e1 = Character.valueOf('\u00E9');
        Character e2 = Character.valueOf('\u00E9');
        System.out.println("equal values for 0xe9: " + e1.equals(e2));
        System.out.println("0xe9 in guaranteed range: " + ('\u00E9' <= '\u007F'));
    }
}

How It Works

  1. Guaranteed sharing. 'A' (65) is inside \u0000 to \u007F, so both calls return the same cached object and == is true.
  2. Outside the guarantee. U+00E9 (233) is above 127; whether e1 == e2 holds depends on the JVM, so the example does not print it.
  3. equals always works. Comparing the values with equals is true whether or not the objects are shared.

Example 3 — Collect the Distinct Letters of a Word

Box each char of a word and keep only the first occurrence in an ArrayList.

Java
import java.util.ArrayList;

public class CharacterValueOfUnique {
    public static void main(String[] args) {
        char[] letters = "hello".toCharArray();
        ArrayList<Character> unique = new ArrayList<Character>();
        for (char c : letters) {
            Character boxed = Character.valueOf(c);
            if (!unique.contains(boxed)) {
                unique.add(boxed);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (Character ch : unique) sb.append(ch).append(' ');
        System.out.println("distinct: " + sb.toString().trim());
        System.out.println("count: " + unique.size());
    }
}

How It Works

  1. Objects for a collection. An ArrayList cannot hold primitive chars, so each one is boxed with valueOf first.
  2. contains compares with equals. The second 'l' matches the first, so it is not added again.
  3. Result. h, e, l, o remain in the order they first appeared: 4 distinct letters.

Frequently Asked Questions

It wraps a primitive char in a Character object, reusing cached objects for chars \u0000 to \u007F.
It is guaranteed to cache \u0000 to \u007F (0 to 127). Other values may be cached, but you cannot rely on it.
Yes. Character c = 'x'; compiles to Character c = Character.valueOf('x');.
No. Use equals. == only works reliably inside the cached ASCII range.
The constructor always creates a new object and has been deprecated since Java 9. valueOf can reuse cached objects.

Back to the method index

See all 52 Character method guides grouped by what they do.

Method index →

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