The java.lang.Character.isLetterOrDigit(char ch) method is an inbuilt method in Java that checks whether a given character is either a letter or a digit.
public class GFG {
public static void main(String[] args) {
char ch = 'A';
System.out.println(Character.isLetterOrDigit(ch));
}
}
Output
true
Explanation: This example checks whether the character 'A' is a letter or digit.
Syntax
public static boolean isLetterOrDigit(char ch)
- Parameters: "ch " The character to be tested.
- Return Value: Returns true if the character is a letter or digit; otherwise, returns false.
Example 1: Checking Letter and Digit Characters
public class GFG {
public static void main(String[] args) {
char c1 = 'Z';
char c2 = '2';
System.out.println(c1 + " is a letter/digit ? " +
Character.isLetterOrDigit(c1));
System.out.println(c2 + " is a letter/digit ? " +
Character.isLetterOrDigit(c2));
}
}
Output
Z is a letter/digit ? true 2 is a letter/digit ? true
Explanation: Both 'Z' (a letter) and '2' (a digit) satisfy the condition, so the method returns true.
Example 2: Checking Letter and Special Character.
public class GFG {
public static void main(String[] args) {
char c1 = 'D';
char c2 = '/';
System.out.println(c1 + " is a letter/digit ? " +
Character.isLetterOrDigit(c1));
System.out.println(c2 + " is a letter/digit ? " +
Character.isLetterOrDigit(c2));
}
}
Output
D is a letter/digit ? true / is a letter/digit ? false
Explanation:
- Two characters are defined: 'D' (a letter) and '/' (a special character).
- Character.isLetterOrDigit() checks each character.
- The method returns true for the letter and false for the special character, and the results are printed.