Home Java Character Methods isValidCodePoint() Java Character isValidCodePoint() Method Definition In Java, Character.isValidCodePoint(codePoint) returns true when the int is between Character.MIN_CODE_POINT (0) and Character.MAX_CODE_POINT (0x10FFFF), inclusive. Those are all the numbers Unicode can ever use.
It is a pure range check. It does not ask whether a character is assigned : unassigned code points and the surrogate range U+D800 to U+DFFF are still "valid" here. Its main job is protecting methods such as toChars() , which throw for anything outside the range.
A static method of java.lang.Character (Java 5+): call it on the class, as in Character.isValidCodePoint(...); you do not need a Character object. No import is needed, because java.lang is always available.
Syntax Pass any int.
Character.isValidCodePoint(0x41); // true
Character.isValidCodePoint(0x10FFFF); // true, the largest code point
Character.isValidCodePoint(0x110000); // false
Character.isValidCodePoint(-1); // false Call What it does if (Character.isValidCodePoint(cp)) ...Guard a conversion of untrusted input. cp >= 0 && cp <= Character.MAX_CODE_POINTThe same check, written out. Character.isValidCodePoint(Integer.parseInt(hex, 16))Validate a hex code typed by a user.
Only one overload exists, isValidCodePoint(int codePoint), added in Java 5.
Library declarations // java.lang.Character (since Java 5)
public static boolean isValidCodePoint(int codePoint)
public static final int MIN_CODE_POINT = 0x000000;
public static final int MAX_CODE_POINT = 0x10FFFF; Parameters Parameter Description codePoint (int)The number to check.
Return Value Return Value Description true0 to 0x10FFFF inclusive. falseNegative, or greater than 0x10FFFF.
Important
Passing an invalid code point to toChars, Character.toString(int), or StringBuilder.appendCodePoint throws IllegalArgumentException; checking with isValidCodePoint first avoids that. A valid code point can still be unassigned or a lone surrogate, so use isDefined() when you need a real character. The method never throws.
Compare with Similar Methods Range checks for code points:
Java Examples 1. Valid or not 2. Validate user input 3. Range constants Example 1 — Code Points at the Edges of the Range Check zero, a letter, a surrogate, the largest code point, one past it, and a negative number.
public class CharacterIsValidCodePointTable {
public static void main(String[] args) {
int[] values = {0, 'A', 0xD800, 0x10FFFF, 0x110000, -1};
for (int v : values) {
System.out.println(v + " valid=" + Character.isValidCodePoint(v));
}
}
} 0 valid=true
65 valid=true
55296 valid=true
1114111 valid=true
1114112 valid=false
-1 valid=false How It Works Both ends are included. 0 and 1114111 (0x10FFFF) are the smallest and largest valid code points.Surrogates pass. 55296 (0xD800) is reserved for surrogate halves, but it is still inside the range, so the answer is true.Outside the range. 1114112 is one past the maximum, and -1 is negative; both are rejected.Example 2 — Validate Hex Code Points Before Converting Them Parse hex strings typed by a user, reject invalid ones, and convert the rest with toChars.
public class CharacterIsValidCodePointParse {
public static void main(String[] args) {
String[] inputs = {"41", "1F600", "110000"};
for (String hex : inputs) {
int cp = Integer.parseInt(hex, 16);
if (Character.isValidCodePoint(cp)) {
char[] units = Character.toChars(cp);
System.out.println(hex + ": ok, " + units.length + " char(s)");
} else {
System.out.println(hex + ": not a code point");
}
}
}
} 41: ok, 1 char(s)
1F600: ok, 2 char(s)
110000: not a code point How It Works Parse in base 16. Integer.parseInt(hex, 16) turns "1F600" into 128512 and "110000" into 1114112.Check before converting. Only valid values reach Character.toChars, so no IllegalArgumentException can occur.Report the size. units.length shows that U+0041 needs one char and U+1F600 needs two.Example 3 — MIN_CODE_POINT, MAX_CODE_POINT, and What Happens Without a Check Print the range constants, count the valid code points, then call toChars on an invalid value.
public class CharacterIsValidCodePointRange {
public static void main(String[] args) {
System.out.println("MIN_CODE_POINT = " + Character.MIN_CODE_POINT);
System.out.println("MAX_CODE_POINT = 0x" + Integer.toHexString(Character.MAX_CODE_POINT));
System.out.println("valid values: " + (Character.MAX_CODE_POINT - Character.MIN_CODE_POINT + 1));
int bad = Character.MAX_CODE_POINT + 1;
System.out.println("isValidCodePoint(max + 1): " + Character.isValidCodePoint(bad));
try {
Character.toChars(bad);
} catch (IllegalArgumentException e) {
System.out.println("toChars(max + 1): IllegalArgumentException");
}
}
} MIN_CODE_POINT = 0
MAX_CODE_POINT = 0x10ffff
valid values: 1114112
isValidCodePoint(max + 1): false
toChars(max + 1): IllegalArgumentException How It Works Named constants. Character.MIN_CODE_POINT and MAX_CODE_POINT are clearer than writing 0 and 0x10FFFF by hand.17 planes. 0x10FFFF + 1 = 1114112 = 17 * 65536, the number of possible code points.No check, an exception. toChars rejects max + 1 with IllegalArgumentException, which isValidCodePoint would have predicted.Frequently Asked Questions What is the valid range of Unicode code points? 0 to 0x10FFFF (1,114,111), which gives 1,114,112 possible values. Character.MAX_CODE_POINT holds the upper limit.
Are surrogate code points valid? Yes, for isValidCodePoint. U+D800 to U+DFFF are inside the range, even though they are not characters on their own.
Does valid mean the character exists? No. Most valid code points are unassigned. Use isDefined to check whether a character is assigned.
Which methods throw for invalid code points? Character.toChars, Character.toString(int) (Java 11+), and StringBuilder.appendCodePoint throw IllegalArgumentException.
Can isValidCodePoint throw an exception? No. It returns false for any int outside the range.
Next: isWhitespace() Check whether a character is whitespace by Java's rules (spaces, tabs, line breaks).
isWhitespace() tutorial → 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.
11 people found this page helpful
Helpful Share Copy link Suggestion