Java Character charValue() Method

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

Definition

In Java, charValue() is called on a Character object and returns the primitive char it holds. Character is the wrapper class for char: it lets a single character live in collections such as ArrayList<Character>, which cannot store primitives.

Since Java 5 the compiler calls charValue() for you whenever a Character is used where a char is needed. This is called auto-unboxing: char c = boxed; means char c = boxed.charValue();.

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

Syntax

Call it on a Character object; it takes no arguments.

Java
Character boxed = Character.valueOf('J');
char c = boxed.charValue();   // 'J'
char d = boxed;               // auto-unboxing: the compiler calls charValue()
CallWhat it does
boxed.charValue()Explicitly unwrap the char.
char c = boxed;Implicit unwrapping (Java 5+).
boxed != null ? boxed.charValue() : '-'Unwrap safely when the value might be missing.

There is exactly one form, charValue(), available since Java 1.0.

Library declarations
Java
// java.lang.Character (since Java 1.0)
public char charValue()

Parameters

ParameterDescription
(none)The method takes no parameters.

Return Value

Return ValueDescription
charThe primitive value this Character object represents.

Compare with Similar Methods

Other ways to get from Character to char or back:

MethodHow it differs
Character.valueOf(c)The reverse: wraps a char in a Character.
boxed.toString()Turns the object into a one-char String.
a.compareTo(b)Compares two objects without unwrapping them yourself.

Java Examples

Example 1 — Unwrap a Character and Use It as a Number

Get the primitive char back, print it, and do arithmetic with it.

Java
public class CharacterCharValueBasic {
    public static void main(String[] args) {
        Character boxed = Character.valueOf('J');
        char c = boxed.charValue();
        System.out.println("char: " + c);
        System.out.println("code: " + (int) c);
        System.out.println("next: " + (char) (c + 1));
    }
}

How It Works

  1. From object to primitive. boxed is a Character object; charValue() returns the plain char 'J' inside it.
  2. A char is a number. Casting to int shows its code, 74. Adding 1 gives 75, and casting back to char gives 'K'.
  3. Why the cast back is needed. c + 1 is an int; without (char) the program would print 75 instead of a letter.

Example 2 — Unwrap Each Character from an ArrayList

Collections store objects, so a list of letters holds Character values. Unwrap them to add up their codes and convert them.

Java
import java.util.ArrayList;

public class CharacterCharValueList {
    public static void main(String[] args) {
        ArrayList<Character> letters = new ArrayList<Character>();
        letters.add('a');
        letters.add('b');
        letters.add('c');
        int total = 0;
        StringBuilder upper = new StringBuilder();
        for (Character ch : letters) {
            char c = ch.charValue();
            total += c;
            upper.append(Character.toUpperCase(c));
        }
        char first = letters.get(0);
        System.out.println("sum of codes: " + total);
        System.out.println("upper: " + upper);
        System.out.println("auto-unboxed first: " + first);
    }
}

How It Works

  1. Boxing on the way in. letters.add('a') wraps each char into a Character automatically.
  2. Unboxing on the way out. ch.charValue() gives back the primitive: 97 + 98 + 99 = 294, and each letter is upper-cased.
  3. The implicit form. char first = letters.get(0); compiles to a charValue() call, so it prints a without any visible method call.

Example 3 — A Missing Map Value: charValue() on null

Look up a grade that exists and one that does not, then unwrap both.

Java
import java.util.HashMap;
import java.util.Map;

public class CharacterCharValueNull {
    public static void main(String[] args) {
        Map<String, Character> grades = new HashMap<String, Character>();
        grades.put("ann", 'A');
        System.out.println("ann: " + grades.get("ann").charValue());
        Character missing = grades.get("bob");
        try {
            char c = missing.charValue();
            System.out.println("bob: " + c);
        } catch (NullPointerException e) {
            System.out.println("bob: NullPointerException");
        }
        char safe = missing != null ? missing.charValue() : '-';
        System.out.println("bob (safe): " + safe);
    }
}

How It Works

  1. get returns an object. grades.get("ann") is a Character, so charValue() unwraps 'A'.
  2. No entry means null. There is no key "bob", so missing is null and calling a method on it throws.
  3. Check before unwrapping. The conditional expression only calls charValue() when the value exists and otherwise uses '-'.

Frequently Asked Questions

The primitive char stored inside the Character object.
Rarely. Since Java 5, auto-unboxing calls it for you when a Character is assigned to a char or used in arithmetic.
charValue() throws NullPointerException, and so does auto-unboxing, because it calls the same method.
No. It is an instance method: call it on a Character object, as in boxed.charValue().
Use Character.valueOf(c), or simply assign the char to a Character variable and let autoboxing do it.

Next: codePointAt()

Read the full code point at an index of a char sequence or char array.

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

8 people found this page helpful