Java ProgramsControl FlowCheck Character Is Alphabet

Check Character Is Alphabet in Java

beginner·  Control Flow  ·  Conditional Statements

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.

Input
'K'
Output
'K' is an alphabet: true

Java Program

Java
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

'K' is an alphabet: true

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.

How It Works
  1. 1(ch >= 'a' && ch <= 'z') checks the lowercase range.
  2. 2(ch >= 'A' && ch <= 'Z') checks the uppercase range, combined with ||.
  3. 3Because Java characters are backed by numeric codes, these comparisons work the same way integer range checks do.
For 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

character range checklogical ORASCII ordering

Approach 2: Using Character.isLetter()

Java
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

'K' is an alphabet: true

Core Logic

Java's Character class already has a built-in check for exactly this, covering more than just the plain ASCII alphabet.

How It Works
  1. 1Character.isLetter(ch) reports whether the character is classified as a letter.
  2. 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.

Key Concepts

Character.isLetter()wrapper class methods

Related Programs