Java Character isDigit() Example

Java Character API comes with many methods to test whether a character is a digit or letter etc. This type of testing is useful in situations like constraints on passwords, employee id etc.

To test for digit, the character class comes with isDigit(char) method.

Following is the method signature as defined in java.lang.Character class.

  • public static boolean isDigit(char ch): Checks the specified character is a digit or not. The method returns true if the char ch is a digit else false.
Following Java Character isDigit() Example illustrates. Just for logic point of view, special characters are also checked.
public class CharacterFunctionDemo
{
  public static void main(String args[])
  {
 
    char ch1 = '5';                            // observe, it is digit
    char ch2 = 'A';                            // observe, it is not digit, it is letter

     boolean b1 = Character.isDigit(ch1);
     boolean b2 = Character.isDigit(ch2);

     System.out.println(ch1 + " is digit: " + b1);     
     System.out.println(ch2 + " is digit: " + b2);     

     System.out.println("\n+ is digit: " + Character.isDigit('+'));       // observe, it is not digit, it is special character
     System.out.println("$ is digit: " + Character.isDigit('$'));         // observe, it is not digit, it is special character
  }
}


ima
Output screenshot on Java Character isDigit() Example

Practice other methods of Character class and also the methods of Math, GregorianCalendar, String and StringBuffer for command on Java coding, especially Beginners.

3 thoughts on “Java Character isDigit() Example”

Leave a Comment

Your email address will not be published.