Java Character isLetterOrDigit() Method

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

Definition

In Java, Character.isLetterOrDigit(ch) returns true when the character is a letter or a decimal digit. It gives exactly the same answer as Character.isLetter(ch) || Character.isDigit(ch), in a single call.

It is the usual building block for "alphanumeric" checks, such as usernames or word splitting. Like its two parts, it understands every script, so '\u00E9' (e with acute) and Arabic-Indic digits pass, while spaces, punctuation, and the underscore do not.

A static method of java.lang.Character (Java 1.0+): call it on the class, as in Character.isLetterOrDigit(...); 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 one char or int code point.

Java
Character.isLetterOrDigit('k');   // true  (letter)
Character.isLetterOrDigit('8');   // true  (digit)
Character.isLetterOrDigit('_');   // false (punctuation)
Character.isLetterOrDigit(' ');   // false
CallWhat it does
Character.isLetterOrDigit(c)Alphanumeric test for one character.
!Character.isLetterOrDigit(c)Find separators such as spaces and punctuation.
Character.isLetterOrDigit(s.codePointAt(i))The same test on a full code point.

Overloads: isLetterOrDigit(char ch) and isLetterOrDigit(int codePoint).

Library declarations
Java
// java.lang.Character
public static boolean isLetterOrDigit(char ch)
public static boolean isLetterOrDigit(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
trueisLetter or isDigit would return true.
falseSpaces, punctuation, symbols, control codes, and other numbers such as Roman numerals.

Compare with Similar Methods

How the related tests split the work:

MethodHow it differs
isLetter(ch)The letter half on its own.
isDigit(ch)The digit half on its own.
isJavaIdentifierPart(ch)Also accepts _, $, and combining marks: what may appear inside a Java name.
isAlphabetic(cp)A wider letter test (letter numbers, vowel signs), but no digits.

Java Examples

Example 1 — Accept Only Letters and Digits in a Username

Reject a name as soon as one character is neither a letter nor a digit.

Java
public class CharacterIsLetterOrDigitUsername {
    public static void main(String[] args) {
        String[] names = {"duke42", "duke_42", "duke 42", "42"};
        for (String name : names) {
            boolean ok = !name.isEmpty();
            for (char c : name.toCharArray()) {
                if (!Character.isLetterOrDigit(c)) {
                    ok = false;
                    break;
                }
            }
            System.out.println("\"" + name + "\" alphanumeric? " + ok);
        }
    }
}

How It Works

  1. Empty is not valid. ok starts as !name.isEmpty(), so an empty name would print false even though the loop never runs.
  2. First bad character wins. In "duke_42" the underscore fails the test, ok becomes false, and break skips the remaining characters. The space in "duke 42" fails the same way.
  3. Digits alone are fine. "42" passes because digits count; add a separate isLetter check on the first character if a name must start with a letter.

Example 2 — Split Text into Alphanumeric Words

Treat every run of letters and digits as a word and everything else as a separator.

Java
public class CharacterIsLetterOrDigitWords {
    public static void main(String[] args) {
        String text = "Hi!! Java 8, (and) 11...";
        StringBuilder words = new StringBuilder();
        int count = 0;
        boolean inWord = false;
        for (char c : text.toCharArray()) {
            if (Character.isLetterOrDigit(c)) {
                if (!inWord) {
                    count++;
                    if (count > 1) {
                        words.append('|');
                    }
                    inWord = true;
                }
                words.append(c);
            } else {
                inWord = false;
            }
        }
        System.out.println(count + " words: " + words);
    }
}

How It Works

  1. A flag tracks word boundaries. inWord is false between words, so the first alphanumeric character after a separator starts a new word and bumps count.
  2. Separators reset the flag. Spaces, !, ,, the parentheses, and the dots all fail isLetterOrDigit, however many appear in a row.
  3. A bar between words. From the second word on, a '|' is appended before the first letter, which gives the five words Hi, Java, 8, and, 11.

Example 3 — One Call vs Two: A Side-by-Side Table

Print all three tests for a letter, a digit, a hyphen, and three non-ASCII number-like characters.

Java
public class CharacterIsLetterOrDigitTable {
    public static void main(String[] args) {
        char[] chars = {'Q', '3', '-', '\u00B2', '\u0663', '\u2160'};
        for (char c : chars) {
            System.out.println((int) c + " letterOrDigit=" + Character.isLetterOrDigit(c)
                    + " letter=" + Character.isLetter(c) + " digit=" + Character.isDigit(c));
        }
    }
}

How It Works

  1. Always the OR of the other two. In every row, letterOrDigit is true exactly when letter or digit is true.
  2. A non-ASCII digit passes. Arabic-Indic three (1635) is a decimal digit, so it counts as alphanumeric.
  3. Number-like, yet rejected. Superscript two (178) and ROMAN NUMERAL ONE (8544) are neither letters nor decimal digits, so all three tests say false.

Frequently Asked Questions

Yes. The documentation defines it that way: it returns true when either isLetter or isDigit would return true.
No. The underscore is connector punctuation. Use isJavaIdentifierPart if you want letters, digits, and underscores.
No. By default \w matches [a-zA-Z_0-9]: it includes the underscore and excludes non-ASCII letters, while isLetterOrDigit does the opposite on both points.
Yes. Characters such as U+00E9 (e with acute) are lowercase letters, so they return true.
Loop over the characters and return false at the first one that fails isLetterOrDigit. Decide separately whether an empty string should count as valid.

Next: isLowerCase()

Check whether a character is lowercase, such as a to z.

isLowerCase() 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.

8 people found this page helpful