In Java, Character.codePointOf(name) is the reverse of getName(): you give it a Unicode character name and it returns the matching code point as an int. codePointOf("LATIN SMALL LETTER A") returns 97.
Matching ignores case and removes leading and trailing whitespace, so "latin small letter a" works too. This lets you write readable code such as codePointOf("GRINNING FACE") instead of a bare 0x1F600.
A static method of java.lang.Character (Java 9+): call it on the class, as in Character.codePointOf(...); you do not need a Character object. No import is needed, because java.lang is always available.
Java 9+ only.
Character.codePointOf(String name) was added in Java 9, so the examples on this page need Java 9 or newer. Java 8 has no built-in reverse lookup; a common workaround is to build your own map once by looping over all code points and calling Character.getName (Java 7+).
Syntax
Pass the character name as a String.
Java
int a = Character.codePointOf("LATIN SMALL LETTER A"); // 97
int b = Character.codePointOf("grinning face"); // 128512 (0x1F600), case ignored
Character.codePointOf("NO SUCH NAME"); // throws IllegalArgumentException
Call
What it does
Character.codePointOf(name)
Name to code point.
Character.toChars(Character.codePointOf(name))
Name to char[], ready for a String.
Character.getName(Character.codePointOf(name))
Normalise a name to its official uppercase form.
One overload, codePointOf(String name), added in Java 9.
Library declarations
Java
// java.lang.Character (since Java 9)
public static int codePointOf(String name)
Parameters
Parameter
Description
name (String)
A Unicode character name; case and surrounding whitespace are ignored.
Look up four names and print the code points in decimal and hex.
Java
public class CharacterCodePointOfLookup {
public static void main(String[] args) {
String[] names = {"LATIN SMALL LETTER A", "DIGIT ZERO", "SPACE", "GRINNING FACE"};
for (String n : names) {
int cp = Character.codePointOf(n);
System.out.println(n + " -> " + cp + " (0x" + Integer.toHexString(cp) + ")");
}
}
}
Output
LATIN SMALL LETTER A -> 97 (0x61)
DIGIT ZERO -> 48 (0x30)
SPACE -> 32 (0x20)
GRINNING FACE -> 128512 (0x1f600)
How It Works
Readable constants. Each name is turned into its number, so code can say what it means instead of using magic numbers.
ASCII and beyond. The first three are ASCII; GRINNING FACE is U+1F600, outside the BMP.
Hex for comparison.Integer.toHexString shows the familiar U+ form of each result.
Example 2 — Names Are Matched Ignoring Case and Outer Spaces
Look up names written in lowercase, mixed case, and with extra spaces, then normalise them with getName.
Java
public class CharacterCodePointOfCase {
public static void main(String[] args) {
String[] inputs = {"latin capital letter a", " Dollar Sign ", "Thumbs Up Sign"};
for (String in : inputs) {
int cp = Character.codePointOf(in);
System.out.println("[" + in + "] -> " + cp + " -> " + Character.getName(cp));
}
}
}
Output
[latin capital letter a] -> 65 -> LATIN CAPITAL LETTER A
[ Dollar Sign ] -> 36 -> DOLLAR SIGN
[Thumbs Up Sign] -> 128077 -> THUMBS UP SIGN
How It Works
Case does not matter."latin capital letter a" finds 65, the same as the uppercase name.
Outer spaces are trimmed. The brackets show the extra spaces around Dollar Sign; the lookup still finds 36.
Back to the official form.getName returns the canonical uppercase name, a simple way to normalise user input.
Example 3 — Handle Unknown Names and null
Build a String from a name, then pass a name that does not exist and a null.
Java
public class CharacterCodePointOfErrors {
public static void main(String[] args) {
String smile = new String(Character.toChars(Character.codePointOf("GRINNING FACE")));
System.out.println("built from name, length " + smile.length());
try {
Character.codePointOf("HAPPY FACE WITH HAT");
} catch (IllegalArgumentException e) {
System.out.println("unknown name: IllegalArgumentException");
}
try {
Character.codePointOf(null);
} catch (NullPointerException e) {
System.out.println("null: NullPointerException");
}
}
}
Output
built from name, length 2
unknown name: IllegalArgumentException
null: NullPointerException
How It Works
Name to text.toChars turns the code point into a surrogate pair, and new String wraps it: length 2.
No guessing. There is no character called HAPPY FACE WITH HAT, so the method throws instead of returning a near match.
null is rejected. Passing null throws NullPointerException; validate user input before the lookup.
Frequently Asked Questions
Java 9. It does not exist in Java 8.
No. Matching ignores case and removes leading and trailing whitespace.
It throws IllegalArgumentException.
Build a map from Character.getName(cp) to cp by looping over all code points once (getName exists since Java 7), then look names up in the map.
new String(Character.toChars(Character.codePointOf(name))), or on Java 11+ the static toString(int) overload.