Java Character isDigit() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Character

Definition

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.

Syntax

Pass a char or an int code point.

Java
Character.isDigit('7');        // true
Character.isDigit('x');        // false
Character.isDigit('\u0663');   // true, ARABIC-INDIC DIGIT THREE
Character.isDigit(0x1D7CE);    // true, MATHEMATICAL BOLD DIGIT ZERO (int overload)
CallWhat it does
Character.isDigit(c)Test one character.
if (Character.isDigit(c)) n = n * 10 + (c - '0');Build a number from ASCII digit characters.
c >= '0' && c <= '9'The plain ASCII-only check, when other scripts must be rejected.

Overloads: isDigit(char ch) and isDigit(int codePoint). To get the value of a digit in any script, use Character.digit(ch, 10) rather than c - '0'.

Library declarations
Java
// java.lang.Character
public static boolean isDigit(char ch)
public static boolean isDigit(int codePoint)   // since Java 5

Parameters

ParameterDescription
ch (char)The character to test.
codePoint (int)The Unicode code point to test.

Return Value

Return ValueDescription
trueThe character is a decimal digit (category DECIMAL_DIGIT_NUMBER).
falseAnything else, including letters such as 'a' that are digits in hexadecimal.

Compare with Similar Methods

Related ways to recognise digits:

MethodHow it differs
isLetterOrDigit(ch)True for letters as well as digits.
digit(ch, radix)Returns the digit's value in a radix, or -1; accepts a to z for radix above 10.
getNumericValue(ch)Also gives values for Roman numerals and other numeric characters.
c >= '0' && c <= '9'ASCII digits only.

Java Examples

Example 1 — Check That a PIN Has Exactly Four Digits

A PIN is valid when it has length 4 and every character passes isDigit.

Java
public class CharacterIsDigitPin {
    public static void main(String[] args) {
        String[] pins = {"2580", "25a0", "258", "0000"};
        for (String pin : pins) {
            boolean ok = pin.length() == 4;
            for (int i = 0; i < pin.length() && ok; i++) {
                ok = Character.isDigit(pin.charAt(i));
            }
            System.out.println(pin + " valid? " + ok);
        }
    }
}

How It Works

  1. Length first. ok starts as pin.length() == 4, so "258" is rejected before any character is looked at.
  2. Stop at the first failure. The loop condition includes && ok, so for "25a0" it ends as soon as 'a' fails isDigit.
  3. 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);
    }
}

How It Works

  1. Skip the rest. Letters, spaces, and the comma fail isDigit, so only 2, 0, 4, and 2 reach the body of the if.
  2. Shift and add. number * 10 + digit grows the value step by step: 2, then 20, then 204, then 2042.
  3. 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);
        }
    }
}

How It Works

  1. 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.
  2. Only one is ASCII. The range check accepts just '5'; pick it when a later step, such as c - '0', assumes ASCII.
  3. 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.

Next: isHighSurrogate()

Check whether a char is the first half of a surrogate pair (0xD800 to 0xDBFF).

isHighSurrogate() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful