Character Class Methods in Java

Naveen AnamaneniUpdated · 3 min read

A password rule says "at least one capital letter, one digit and one symbol". To check it, you look at the password one character at a time and ask: is this a letter? A digit? Upper case? The Character class (the wrapper for char) answers exactly these questions, and it understands letters and digits from every language, not just English.

Checking a Password

Password.java▶ Try it
public class Password {
    public static void main(String[] args) {
        String pwd = "Navika@2026";
        int upper = 0, lower = 0, digits = 0, other = 0;
 
        for (char c : pwd.toCharArray()) {
            if (Character.isUpperCase(c)) {
                upper++;
            } else if (Character.isLowerCase(c)) {
                lower++;
            } else if (Character.isDigit(c)) {
                digits++;
            } else {
                other++;
            }
        }
        System.out.println("Upper " + upper + ", lower " + lower
            + ", digits " + digits + ", symbols " + other);
 
        boolean strong = upper > 0 && digits > 0 && other > 0
            && pwd.length() >= 8;
        System.out.println("Strong: " + strong);
    }
}
Output
Upper 1, lower 5, digits 4, symbols 1
Strong: true

How it works

  1. toCharArray() gives the characters one by one.
  2. Character.isUpperCase, isLowerCase and isDigit classify each one. Anything left over (here @) counts as a symbol.
  3. The password passes all four rules, so it's strong.

Quick Reference

Method'N''a''7'' ''_'
isLetter(c)truetruefalsefalsefalse
isDigit(c)falsefalsetruefalsefalse
isLetterOrDigit(c)truetruetruefalsefalse
isUpperCase(c)truefalsefalsefalsefalse
isWhitespace(c)falsefalsefalsetruefalse
toUpperCase(c)NA7' '_

These methods work for every language: Character.isLetter('अ') (a Hindi letter) is true, and Character.isDigit('٣') (the Arabic digit three) is true as well.

A char Is Secretly a Number

Every char is stored as a number, its Unicode code: 'A' is 65, 'a' is 97 and '7' is 55. That's why character arithmetic works:

ExpressionResultWhy
(int) 'A'65The code of A
(char) ('a' + 1)bOne code after a
'7' - '0'755 minus 48: the digit's value
Character.getNumericValue('7')7The same, written clearly

Common Mistakes

Comparing with c >= 'A' && c <= 'Z' for "is a letter". It misses lowercase and every non-English letter; use Character.isLetter.

Treating '7' as the number 7. It's code 55; convert with c - '0' or getNumericValue.

Forgetting that toUpperCase returns a value. Character.toUpperCase(c); alone doesn't change c.

Practice

  1. Easy: Count the vowels, consonants, digits and spaces in "Advitha joined in 2026".
  2. Harder: Write static boolean isValidUsername(String u) that allows only letters, digits and underscores, must start with a letter, and must be 4 to 12 characters long.

Quick check: Character class

4 questions
1What does Character.isDigit('5') return?
2What is '7' - '0'?
3Which works for letters in every language?
4What does (char) ('a' + 2) give?

Key Takeaways

Was this tutorial helpful?

Lesson 196 of 482