Java Program to check whether a given character is alphabet or not

program to check whether a character is alphabet or not

What is  a Character?

In Java, a char is a data type that represents a single character. It is a type of primitive data type.
The char data type is used to store character values, such as letters, digits, and symbols. It is represented by the char keyword in Java and can hold any value in the Unicode character set, which includes characters from many different languages and scripts.

In this Article, we will write a program using different methods to check whether a given character is alphabet or not.

What is an Alphabet in Java?

In Java, the alphabet refers to the set of letters that are used in the language to represent characters. In the Java programming language, characters are represented using the char data type, which is a 16-bit Unicode character. The alphabet in Java includes the 26 letters of the English alphabet (A-Z and a-z), as well as various symbols and special characters.

Program to check whether a given character is alphabet or not:

we can check a character for the alphabet using multiple methods, in which some of the method by which we can check whether a given character is alphabet or not by:

  • using if else statements.
  • using Character.isAlphabetic() method.
  • using isLetter() method.

Example using if-else statements:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Initializing a character
        char Prep = 'A';

        // if-else to check whether a character is alphabet or not
        if( (Prep >= 'a' && Prep <= 'z') || (Prep >= 'A' && Prep <= 'Z'))
            // If prep is an alphabet
            System.out.println(Prep + " is an alphabet.");
        else
            // if prep is not an alphabet
            System.out.println(Prep + " is not an alphabet.");
    }
}
Output:

A is an alphabet.

Explanation:

Each ASCII character is assigned a unique numerical value between 0 and 127. For example, the ASCII value of the letter “A” is 65, the ASCII value of the letter “B” is 66, and so on. The ASCII value of lowercase alphabets are from 97 to 122 and, the ASCII value of uppercase alphabets are from 65 to 90.
Since the ASCII value of A fall in between the ASCII value of alphabets. Therefore, the program gives output A is an alphabet.

Example using Character.isAlphabetic() method:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Initializing a character
        char Prep = 'a';
        
        // returning the isAlphabet
        boolean ans = Character.isAlphabetic(Prep);
        
        // if else to print whether a character is alphabet or not
        if (ans == true){
            System.out.println(Prep + " is an alphabet.");
        } else{
            System.out.println(Prep + " is not an alphabet.");
        }
    }
}
Output:

a is an alphabet.

Explanation:

To check if a character is an alphabet or not in Java, you can use the Character.isAlphabetic() method. This method returns true if the character is an alphabet and false if it is not.

Example using isLetter() method:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Initializing a character
        char Prep = 'a';
        
        // returning value for isLetter function
        boolean ans = Character.isLetter(Prep);
        
        // if else to check whether given character is alphabet or not
        if (ans == true) {
            System.out.println(Prep + " is an alphabet.");
        } else {
            System.out.println(Prep + " is not an alphabet.");
        }
    }
}
 Output:

a is an alphabet.

Explanation:

To check if a character is an alphabet or not in Java, we can also use the isLetter() method of the Character class to check if a character is an alphabet or not. This method also returns true if the character is an alphabet and false if it is not.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription