Home Java Character Methods charValue() Java Character charValue() Method 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.
Character boxed = Character.valueOf('J');
char c = boxed.charValue(); // 'J'
char d = boxed; // auto-unboxing: the compiler calls charValue() Call What 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.lang.Character (since Java 1.0)
public char charValue() Parameters Parameter Description (none) The method takes no parameters.
Return Value Return Value Description charThe primitive value this Character object represents.
Important
Calling it on a null reference throws NullPointerException, and so does auto-unboxing a null, because the compiler inserts the same call. This often happens with values read from a map or list that were never set.
Compare with Similar Methods Other ways to get from Character to char or back:
Java Examples 1. Unwrap a Character 2. Characters in a list 3. Missing values Example 1 — Unwrap a Character and Use It as a Number Get the primitive char back, print it, and do arithmetic with it.
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 From object to primitive. boxed is a Character object; charValue() returns the plain char 'J' inside it.A char is a number. Casting to int shows its code, 74. Adding 1 gives 75, and casting back to char gives 'K'.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.
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);
}
} sum of codes: 294
upper: ABC
auto-unboxed first: a How It Works Boxing on the way in. letters.add('a') wraps each char into a Character automatically.Unboxing on the way out. ch.charValue() gives back the primitive: 97 + 98 + 99 = 294, and each letter is upper-cased.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.
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);
}
} ann: A
bob: NullPointerException
bob (safe): - How It Works get returns an object. grades.get("ann") is a Character, so charValue() unwraps 'A'.No entry means null. There is no key "bob", so missing is null and calling a method on it throws.Check before unwrapping. The conditional expression only calls charValue() when the value exists and otherwise uses '-'.Frequently Asked Questions What does charValue() return? The primitive char stored inside the Character object.
Do I ever need to call charValue() myself? Rarely. Since Java 5, auto-unboxing calls it for you when a Character is assigned to a char or used in arithmetic.
What happens if the Character is null? charValue() throws NullPointerException, and so does auto-unboxing, because it calls the same method.
Is charValue() static? No. It is an instance method: call it on a Character object, as in boxed.charValue().
How do I go the other way, from char to Character? Use Character.valueOf(c), or simply assign the char to a Character variable and let autoboxing do it.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion