In Java, Character.isDigit(ch) returns true when the character is a decimal digit, meaning its general category is DECIMAL_DIGIT_NUMBER. The ASCII digits '0' to '9' are the familiar ones.
Other scripts have their own digits too, such as ARABIC-INDIC DIGIT THREE (U+0663) and FULLWIDTH DIGIT SEVEN (U+FF17), and isDigit accepts them. Characters that only look numeric, such as superscript two (U+00B2) or the Roman numeral U+2167, are not decimal digits and return false.
A static method of java.lang.Character (Java 1.0+): call it on the class, as in Character.isDigit(...); 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.
Length first.ok starts as pin.length() == 4, so "258" is rejected before any character is looked at.
Stop at the first failure. The loop condition includes && ok, so for "25a0" it ends as soon as 'a' fails isDigit.
Zeros are digits."0000" passes: leading zeros do not matter, because each character is tested on its own.
Example 2 — Collect the Digits Hidden in a Sentence
Join every digit character in a string into one number and count how many were found.
Java
public class CharacterIsDigitExtract {
public static void main(String[] args) {
String text = "Room 204, floor 2";
int number = 0;
int digitCount = 0;
for (char c : text.toCharArray()) {
if (Character.isDigit(c)) {
number = number * 10 + (c - '0');
digitCount++;
}
}
System.out.println("digits found: " + digitCount);
System.out.println("joined number: " + number);
}
}
Output
digits found: 4
joined number: 2042
How It Works
Skip the rest. Letters, spaces, and the comma fail isDigit, so only 2, 0, 4, and 2 reach the body of the if.
Shift and add.number * 10 + digit grows the value step by step: 2, then 20, then 204, then 2042.
Why c - '0' works here. The ASCII digits have the consecutive codes 48 to 57, so subtracting '0' (48) leaves the digit value. That shortcut is only safe because the input is plain ASCII.
Example 3 — isDigit vs an ASCII Range Check
Compare isDigit with a plain '0' to '9' check on digits, look-alikes, and a letter.
Java
public class CharacterIsDigitUnicode {
public static void main(String[] args) {
char[] tests = {'5', '\u0663', '\uFF17', '\u00B2', '\u2167', 'x'};
for (char c : tests) {
boolean ascii = c >= '0' && c <= '9';
System.out.println((int) c + " isDigit=" + Character.isDigit(c) + " ascii=" + ascii);
}
}
}
Three real digits.'5', Arabic-Indic three (code 1635), and fullwidth seven (code 65303) all have the category DECIMAL_DIGIT_NUMBER, so isDigit accepts them.
Only one is ASCII. The range check accepts just '5'; pick it when a later step, such as c - '0', assumes ASCII.
Numeric, but not decimal digits. Superscript two (178) and Roman numeral eight (8551) carry numeric values, yet their categories are OTHER_NUMBER and LETTER_NUMBER, so both answers are false.
Frequently Asked Questions
No. It accepts every Unicode decimal digit, including Arabic-Indic, Devanagari, and fullwidth digits. Use c >= '0' && c <= '9' if you need ASCII only.
Not for isDigit, which only accepts decimal digits. Character.digit('f', 16) returns 15, which is the right test for hex.
U+00B2 has the category OTHER_NUMBER, not DECIMAL_DIGIT_NUMBER. getNumericValue still returns 2 for it.
Use Character.digit(c, 10), which works for any script and returns -1 for non-digits. c - '0' is fine only for ASCII digits.
No. Both overloads return false for anything that is not a decimal digit, including invalid code points.