Home Java Character Methods getType() Java Character getType() Method Definition In Java, Character.getType(ch) returns the character's Unicode general category : the basic kind of character it is. The result is an int that matches one of the constants in Character, such as UPPERCASE_LETTER (1), DECIMAL_DIGIT_NUMBER (9), SPACE_SEPARATOR (12), or MATH_SYMBOL (25).
Many other methods are built on it: isUpperCase() and isDigit() , for example, are essentially "is the type this category?". Code points with no character assigned return UNASSIGNED (0).
A static method of java.lang.Character (Java 1.1+): call it on the class, as in Character.getType(...); 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; compare the result with the constants.
Character.getType('A') == Character.UPPERCASE_LETTER // true (1)
Character.getType('7') == Character.DECIMAL_DIGIT_NUMBER // true (9)
Character.getType(' ') == Character.SPACE_SEPARATOR // true (12)
Character.getType('+') == Character.MATH_SYMBOL // true (25)
Character.getType(0x10FFFF) == Character.UNASSIGNED // true (0) Call What it does Character.getType(c) == Character.OTHER_PUNCTUATIONTest for one category. switch (Character.getType(c)) { case Character.UPPERCASE_LETTER: ... }Branch on several categories. Character.getType(s.codePointAt(i))Category of a character above U+FFFF.
Two overloads: getType(char ch) since Java 1.1 and getType(int codePoint) since Java 5. The constants are byte values, but the method returns int.
Library declarations // java.lang.Character
public static int getType(char ch) // Java 1.1
public static int getType(int codePoint) // Java 5
public static final byte UNASSIGNED = 0, UPPERCASE_LETTER = 1, LOWERCASE_LETTER = 2;
public static final byte DECIMAL_DIGIT_NUMBER = 9, SPACE_SEPARATOR = 12, MATH_SYMBOL = 25; Parameters Parameter Description ch (char)The character to classify. codePoint (int)A Unicode code point.
Return Value Return Value Description intOne of the 30 category constants: 0 to 30, with 17 unused.
Compare with Similar Methods Methods built on the same data:
Java Examples 1. Categories of common chars 2. Count categories 3. Beyond ASCII Example 1 — The General Category of Eight ASCII Characters Classify letters, a digit, a space, and four kinds of symbols and punctuation, printing the number and constant name.
public class CharacterGetTypeTable {
static String name(int type) {
switch (type) {
case Character.UPPERCASE_LETTER: return "UPPERCASE_LETTER";
case Character.LOWERCASE_LETTER: return "LOWERCASE_LETTER";
case Character.DECIMAL_DIGIT_NUMBER: return "DECIMAL_DIGIT_NUMBER";
case Character.SPACE_SEPARATOR: return "SPACE_SEPARATOR";
case Character.MATH_SYMBOL: return "MATH_SYMBOL";
case Character.CURRENCY_SYMBOL: return "CURRENCY_SYMBOL";
case Character.DASH_PUNCTUATION: return "DASH_PUNCTUATION";
case Character.START_PUNCTUATION: return "START_PUNCTUATION";
default: return "other";
}
}
public static void main(String[] args) {
char[] chars = {'A', 'a', '7', ' ', '+', '$', '-', '('};
for (char c : chars) {
int t = Character.getType(c);
System.out.println("'" + c + "' " + t + " " + name(t));
}
}
} 'A' 1 UPPERCASE_LETTER
'a' 2 LOWERCASE_LETTER
'7' 9 DECIMAL_DIGIT_NUMBER
' ' 12 SPACE_SEPARATOR
'+' 25 MATH_SYMBOL
'$' 26 CURRENCY_SYMBOL
'-' 20 DASH_PUNCTUATION
'(' 21 START_PUNCTUATION How It Works Constants as case labels. The category constants are compile-time constants, so they can be used directly in a switch.Symbols are split up. '+' is a math symbol and '$' a currency symbol: two different categories.Punctuation is split too. The hyphen-minus is dash punctuation, while '(' is start punctuation (an opening bracket).Example 2 — Count Each Kind of Character in a Sentence Classify every char of a short sentence and count five categories.
public class CharacterGetTypeCount {
public static void main(String[] args) {
String text = "Hello, World 42!";
int upper = 0, lower = 0, digits = 0, spaces = 0, punct = 0;
for (int i = 0; i < text.length(); i++) {
int t = Character.getType(text.charAt(i));
if (t == Character.UPPERCASE_LETTER) upper++;
else if (t == Character.LOWERCASE_LETTER) lower++;
else if (t == Character.DECIMAL_DIGIT_NUMBER) digits++;
else if (t == Character.SPACE_SEPARATOR) spaces++;
else if (t == Character.OTHER_PUNCTUATION) punct++;
}
System.out.println("upper=" + upper + " lower=" + lower + " digits=" + digits
+ " spaces=" + spaces + " punctuation=" + punct);
}
} upper=2 lower=8 digits=2 spaces=2 punctuation=2 How It Works One call per char. getType runs once for each of the 16 chars, and the result is compared with named constants.Letters by case. H and W are uppercase; e, l, l, o, o, r, l, d are the 8 lowercase letters.Other punctuation. Both ',' and '!' are in the OTHER_PUNCTUATION category, giving 2.Example 3 — Categories of an Emoji, a Surrogate, Private Use, and an Unassigned Code Point Use the int overload, and see what the char overload says about half of a surrogate pair.
public class CharacterGetTypeCodePoints {
public static void main(String[] args) {
System.out.println("U+1F600: " + Character.getType(0x1F600)
+ " other symbol=" + (Character.getType(0x1F600) == Character.OTHER_SYMBOL));
System.out.println("0xd83d char: " + Character.getType('\uD83D')
+ " surrogate=" + (Character.getType('\uD83D') == Character.SURROGATE));
System.out.println("U+E000: " + Character.getType(0xE000)
+ " private use=" + (Character.getType(0xE000) == Character.PRIVATE_USE));
System.out.println("U+10FFFF: " + Character.getType(0x10FFFF)
+ " unassigned=" + (Character.getType(0x10FFFF) == Character.UNASSIGNED));
}
} U+1F600: 28 other symbol=true
0xd83d char: 19 surrogate=true
U+E000: 18 private use=true
U+10FFFF: 0 unassigned=true How It Works The whole emoji. Passed as a code point, U+1F600 is OTHER_SYMBOL (28).Half an emoji. Its high surrogate on its own is only SURROGATE (19), which is why text should be classified by code point.Reserved values. U+E000 is set aside for private use (18), and U+10FFFF is a permanent noncharacter, so it is UNASSIGNED (0) in every Unicode version.Frequently Asked Questions What does Character.getType return? An int for the Unicode general category, matching a constant such as Character.UPPERCASE_LETTER (1) or Character.DECIMAL_DIGIT_NUMBER (9).
How do I turn the number into a name? There is no built-in method. Map the constants yourself, for example with a switch.
What does getType return for an unassigned code point? Character.UNASSIGNED, which is 0.
Why does an emoji char return SURROGATE? Emoji above U+FFFF are two chars. Each half alone is a surrogate; call getType with the code point instead.
Why are the constants bytes when getType returns int? They are declared as byte; they widen to int automatically in comparisons.
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.
12 people found this page helpful
Helpful Share Copy link Suggestion