Check Character Is Alphabet in Java
Problem
A character is a letter of the alphabet when it falls within the range 'a' to 'z' or 'A' to 'Z' — everything else, digits, spaces, symbols, is not.
Given a single character, determine whether it is an alphabet letter.
Java Program
public class AlphabetCheck {
public static void main(String[] args) {
char ch = 'K';
boolean isAlphabet = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
System.out.println("'" + ch + "' is an alphabet: " + isAlphabet);
}
}Output
Core Logic
Characters compare using their underlying numeric codes, so checking whether a character falls between 'a' and 'z', or between 'A' and 'Z', is enough to confirm it's a letter.
- 1
(ch >= 'a' && ch <= 'z')checks the lowercase range. - 2
(ch >= 'A' && ch <= 'Z')checks the uppercase range, combined with||. - 3Because Java characters are backed by numeric codes, these comparisons work the same way integer range checks do.
ch = 'K', it falls between 'A' and 'Z', so the uppercase range check matches.Key Point: Both ranges have to be checked separately — 'z' and 'A' aren't adjacent in character code order, so a single combined range wouldn't work.
Key Concepts
Approach 2: Using Character.isLetter()
public class AlphabetCheckBuiltIn {
public static void main(String[] args) {
char ch = 'K';
boolean isAlphabet = Character.isLetter(ch);
System.out.println("'" + ch + "' is an alphabet: " + isAlphabet);
}
}
Output
Core Logic
Java's Character class already has a built-in check for exactly this, covering more than just the plain ASCII alphabet.
- 1
Character.isLetter(ch)reports whether the character is classified as a letter. - 2Unlike the manual range check, this also recognizes accented and non-Latin letters, not just A-Z and a-z.
Character.isLetter('K') returns true.Key Point: For plain ASCII input the two approaches agree, but Character.isLetter() is the more correct choice whenever the input might include letters outside the basic English alphabet.