Java Character isDefined() Example


Java Character API comes with many methods to test whether a character is a digit or letter or defined in Unicode etc. This type of testing is useful in situations when a Java program can accept a character or not.

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

  • public static boolean isDefined(char ch): Checks the specified character ch is defined in Unicode character set or not. It the letter ch exists in the Unicode character set, the method returns true else false.

Java char size is 2 bytes (in C-lang, char is one byte) and its range is 0 to 65,535. This big range is known as Unicode character range. ASCII range is 0 to 255. All the ASCII characters also exist in Unicode range. We can say, ASCII code is a subset of Unicode. All the digits, special characters and letters we use are existing in Unicode set.

Following example illustrates. Just for interest, special characters are also checked.

public class CharacterFunctionDemo
{
  public static void main(String args[])
  {
   char ch1 = 'A';
   char ch2 = '5';                      

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

   System.out.println(ch1 + " is defined in Unicode character set: " + b1);     
   System.out.println(ch2 + " is defined in Unicode character set: " + b2);     

   System.out.println("\n+  is defined in Unicode character set: " + Character.isDefined('+'));
   System.out.println("$ is defined in Unicode character set: " + Character.isDefined('$'));
  }
}

Java Character isDefined() Example

Leave a Comment

Your email address will not be published.