Java.lang.Character.isDigit() Method

Advertisements


Description

The java.lang.Character.isDigit(char ch) determines if the specified character is a digit.

A character is a digit if its general category type, provided by Character.getType(ch), 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.

Declaration

Following is the declaration for java.lang.Character.isDigit() method

public static boolean isDigit(char ch)

Parameters

  • ch - the character to be tested

Return Value

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

Exception

  • NA

Example

The following example shows the usage of lang.Character.isDigit() method.

package com.tutorialspoint;

import java.lang.*;

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 );
   }
}

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



Advertisements
Advertisements