Check if a Character is Alphabet or not in Java

In this post, I will be sharing how to check if a Character is Alphabet or not in Java. There are three ways to achieve our goal of checking if a Character is Alphabet or not in Java:

1. Using isAlphabetic() method [Recommended]

2. Using if else

3. Using ternary operator

Check if a Character is Alphabet or not in Java

1. Using isAlphabetic() method

According to Oracle docs, the Character.isAlphabetic(int codePoint) method can be used to determine if the specified character(Unicode code point) is an alphabet as shown below in the example:

public class CheckCharacterAlphabet {
    public static void main(String args[]) {
      // Declaring variable    
      char ch = 'J';
      // check if 'J' is an alphabet
      if(Character.isAlphabetic(ch))
      {
          System.out.println(ch + " is an Alphabet");
      }
      else
      {
          System.out.println(ch + " is NOT an Alphabet");
      }
    }
}



Output:
J is an alphabet


We can consider a character as an alphabet if it has the following characteristics:

1. LOWERCASE_LETTER
2. UPPERCASE_LETTER
3. MODIFIER_LETTER
4. TITLECASE_LETTER
5. LETTER_NUMBER
6. OTHER_LETTER or other alphabets defined by the Unicode standard

2. Using if else

We can easily check if a Character is an Alphabet or not in Java by using if else statement. In Java programming language, the char variable stores the ASCII value of a character (a number between 0 and 127) rather than the character itself.

The ASCII value of uppercase alphabets is from 65 to 90. That is alphabet 'A' is stored as 65 and alphabet 'Z' is stored as 90. Similarly, alphabet 'a' is stored as 97, and alphabet 'z' is stored as 122. Now, when we compare variable ch between 'A' to 'Z' and 'a' to 'z', the variable is compared with the ASCII value of the alphabets 65 to 90 and 97 to 122 respectively.

public class CheckCharacterAlphabet2 {
    public static void main(String args[]) 
    {
      // Declare the variable    
      char ch = '#';
      // check if '#' is an alphabet
      if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
      {
          System.out.println(ch + " is an alphabet");
      }
      else
      {
          System.out.println(ch + " is NOT an alphabet");   
      }
    }
}


Output:
# is NOT an alphabet


Since the ASCII value of # does not fall in between the ASCII value of characters 'A' to 'Z' and 'a' to 'z'. Hence, the program outputs # is not an alphabet.

3. Using ternary operator

We can also use the ternary operator to check if a character is an alphabet or not in Java as shown below in the example:

public class CheckCharacterAlphabet3 {
    public static void main(String args[]) {
      // Declaring variable    
      char ch = 'J';
      // check if 'J' is an alphabet
      if(Character.isAlphabetic(ch))
      {
          System.out.println(ch + " is an Alphabet");
      }
      else
      {
          System.out.println(ch + " is NOT an Alphabet");
      }
    }
}


Output:
J is an alphabet


That's all for today, please mention in the comments if you have any questions related to how to check if a character is alphabet or not in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry