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.
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);
}
}
}
Empty is not valid.ok starts as !name.isEmpty(), so an empty name would print false even though the loop never runs.
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.
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);
}
}
Output
5 words: Hi|Java|8|and|11
How It Works
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.
Separators reset the flag. Spaces, !, ,, the parentheses, and the dots all fail isLetterOrDigit, however many appear in a row.
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));
}
}
}
Always the OR of the other two. In every row, letterOrDigit is true exactly when letter or digit is true.
A non-ASCII digit passes. Arabic-Indic three (1635) is a decimal digit, so it counts as alphanumeric.
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.