In Java, Character.isDefined(ch) returns true when the character has an entry in the Unicode data of your JDK, and false when it is unassigned. In other words, it is true whenever getType() is anything other than UNASSIGNED.
"Defined" does not mean "printable". Private use characters (such as U+E000) and surrogate code points (U+D800 to U+DFFF) are defined, even though they have no standard picture. The 66 noncharacters, such as U+FFFE and U+FFFF, are never assigned, so they always return false.
A static method of java.lang.Character (Java 1.0+): call it on the class, as in Character.isDefined(...); you do not need a Character object. No import is needed, because java.lang is always available. The int codePoint overload was added in Java 5.
Unicode version.
The answer comes from the Unicode Character Database built into your JDK, and each Java release follows a specific Unicode version (Java 8, for example, uses Unicode 6.2). The characters used on this page have had the same properties for many Unicode versions, so the examples print the same result on any current JDK.
Syntax
Pass a char or an int code point.
Java
Character.isDefined('a'); // true
Character.isDefined('\uE000'); // true, private use
Character.isDefined('\uFFFF'); // false, a noncharacter
Character.isDefined(0x110000); // false, not a valid code point
Call
What it does
Character.isDefined(c)
Is this char assigned in Unicode?
Character.isDefined(cp)
The same for any code point.
if (!Character.isDefined(c)) c = '?';
Replace unassigned characters while cleaning input.
Overloads: isDefined(char ch) and isDefined(int codePoint). New Unicode versions assign new characters, so a code point that is unassigned on an old JDK can become defined on a newer one.
Library declarations
Java
// java.lang.Character
public static boolean isDefined(char ch)
public static boolean isDefined(int codePoint) // since Java 5
Parameters
Parameter
Description
ch (char)
The character to test.
codePoint (int)
The code point to test. Values outside 0 to 0x10FFFF return false.
Return Value
Return Value
Description
true
The character is assigned, including private use and surrogate code points.
false
The character is unassigned (category UNASSIGNED), a noncharacter, or not a valid code point.
Ordinary letters.a (97) and Greek alpha (945) have full entries in the Unicode data.
Defined without a standard picture. U+E000 (57344) is private use and U+D800 (55296) is a surrogate; both have a category of their own, so they count as defined.
Permanent gaps. U+FFFE and U+FFFF (65534 and 65535) are noncharacters that Unicode promises never to assign, so the answer is false on every JDK.
Example 2 — Code Points Near the Edges of Unicode
Use the int overload on an emoji, the last private use character, the last code point, a value past the end, and -1. Then scan a block of noncharacters.
Java
public class CharacterIsDefinedCodePoints {
public static void main(String[] args) {
int[] cps = {0x1F600, 0x10FFFD, 0x10FFFF, 0x110000, -1};
for (int cp : cps) {
System.out.println(Integer.toHexString(cp) + " defined=" + Character.isDefined(cp));
}
int defined = 0;
for (int cp = 0xFDD0; cp <= 0xFDEF; cp++) {
if (Character.isDefined(cp)) defined++;
}
System.out.println("defined in U+FDD0..U+FDEF: " + defined);
}
}
Output
1f600 defined=true
10fffd defined=true
10ffff defined=false
110000 defined=false
ffffffff defined=false
defined in U+FDD0..U+FDEF: 0
How It Works
An emoji and a private use character. U+1F600 has been assigned since Unicode 6.1 (so even Java 8 knows it), and U+10FFFD belongs to a private use plane.
Noncharacter, out of range, negative. U+10FFFF is a noncharacter, 0x110000 is past the end of Unicode, and -1 (printed by toHexString as ffffffff) is not a code point. None throws; all print false.
A whole block of gaps. U+FDD0 to U+FDEF are 32 more noncharacters, so the loop counts 0 defined code points.
Example 3 — Replace Unassigned Characters with a Question Mark
Before saving text, swap every unassigned char for '?' and report how many were replaced.
Java
public class CharacterIsDefinedClean {
public static void main(String[] args) {
String raw = "OK\uFFFF-\uFFFEgo";
StringBuilder clean = new StringBuilder();
int replaced = 0;
for (char c : raw.toCharArray()) {
if (Character.isDefined(c)) {
clean.append(c);
} else {
clean.append('?');
replaced++;
}
}
System.out.println(clean + " (replaced " + replaced + ")");
}
}
Output
OK?-?go (replaced 2)
How It Works
Seven chars of input. O, K, U+FFFF, the hyphen, U+FFFE, g, and o; the two noncharacters often show up when binary data is decoded as text by mistake.
Keep or replace. Defined characters are copied unchanged; each unassigned one becomes '?' and increments the counter.
StringBuilder in a concatenation.clean + " (replaced " calls the builder's toString() automatically, giving OK?-?go.
Frequently Asked Questions
That the character has an entry in the Unicode data of your JDK. Formally, isDefined is true whenever getType does not return UNASSIGNED.
Yes. Code points such as U+E000 have the category PRIVATE_USE, so isDefined returns true even though no standard glyph exists.
Yes. U+D800 to U+DFFF have the category SURROGATE, so isDefined returns true for them.
Each JDK follows a specific Unicode version, and newer versions assign more characters. A recently added emoji can be unassigned on an older JDK.
No. isValidCodePoint only checks that the number is between 0 and 0x10FFFF. Most valid code points are still unassigned, so isDefined returns false for them.