Java - Character isDigit() Method



The Java Character isDigit() method is used to check whether the specified character is a digit or not.

A character is said to be a digit if its general category type, as obtained from the return value of the Character.getType() method, is DECIMAL_DIGIT_NUMBER.

Some Unicode character ranges that contain digits −

  • '\u0030' through '\u0039', ISO-LATIN-1 digits ('0' through '9')

  • '\u0660' through '\u0669', Arabic-Indic digits

  • '\u06F0' through '\u06F9', Extended Arabic-Indic digits

  • '\u0966' through '\u096F', Devanagari digits

  • '\uFF10' through '\uFF19', Fullwidth digits

Many other character ranges contain digits as well.

Syntax

Following is the syntax for Java Character isDigit() method

public static boolean isDigit(char ch)
(or)
public static boolean isDigit(int codePoint)

Parameters

  • ch − the character to be tested

  • codePoint − the Unicode code point to be tested

Return Value

This method returns a boolean value true if the character is a digit, otherwise false.

Checking a Character is defined in Unicode Example

The following example shows the usage of Java Character isDigit() method. We've created char variables and assigned them char values. Now using isDigit() method, we're checking if the char value is digit or not.

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 char primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = '9';
      ch2 = 'V';

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      // assign isDigit results of ch1, ch2 to b1, b2
      b1 = Character.isDigit(ch1);
      b2 = Character.isDigit(ch2);
      String str1 = ch1 + " is a digit is " + b1;
      String str2 = ch2 + " is a digit is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

9 is a digit is true
V is a digit is false

Checking a CodePoint is defined in Unicode Example

The following example shows the usage of Java Character isDigit() method. We've created int variables and assigned them codepoint values. Now using isDigit() method, we're checking if the codepoint value is digit or not.

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x06f8;
      cp2 = 0x0c12;

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      // assign isDigit results of ch1, ch2 to i1, i2
      b1 = Character.isDigit(cp1);
      b2 = Character.isDigit(cp2);
      String str1 = "cp1 represents a digit is " + b1;
      String str2 = "cp2 represents a digit is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

cp1 represents a digit is true
cp2 represents a digit is false

Checking a Character is defined in Unicode Example

The following example shows the usage of Java Character isDigit() method. We've created a char variable and assigned it a char values. Now using isDigit() method, we're checking if the char value is digit or not.

Since this method returns the values of boolean type, we can use it with conditional statements. An example is shown below.

package com.tutorialspoint;

public  class Demo {
   public static void main (String args[]) {
      Character c1 = new Character('5');
      if(Character.isDigit(c1))
         System.out.println("The input character is a digit");
      else
         System.out.println("The input character is not a digit");
   }
}

Output

Once we compile and run the code above, we get the output given below −

The input character is a digit

Checking a Character is defined in Unicode Example

The following example shows the usage of Java Character isDigit() method. We've created a Character variable and assigned it a Character Object. Now using isDigit() method, we're checking if the Character value is digit or not.

Another example using conditional statements on this method is as follows −

package com.tutorialspoint;

public  class Demo {
   public static void main (String args[]) {
      Character c1 = new Character('a');
      if(Character.isDigit(c1))
         System.out.println("The input character is a digit");
      else
         System.out.println("The input character is not a digit");
   }
}

Output

Once we compile and run the code above, we get the output given below −

The input character is not a digit
java_lang_character.htm
Advertisements