Java - Character isDefined() Method



The Java Character isDefined() method is used to determine if a character is defined in Unicode.

A character is said to be defined if at least one of the following is true −

  • It has an entry in the UnicodeData file.

  • It has a value in a range defined by the UnicodeData file.

Note − This method occurs in two polymorphic forms. If the argument passed to the method is a character, it cannot handle supplementary characters. However, they can be supported using another form of this method that takes a code point as an argument.

Syntax

Following is the syntax for Java Character isDefined() method

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

Parameters

  • ch − the character to be tested

  • codePoint − the Unicode code point to be tested

Return Value

This method returns true if the character is defined in Unicode, otherwise false.

Example

The following example shows the usage of Java Character isDefined(char ch) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create a char primitive ch
      char ch;

      // assign value to ch
      ch = '@';

      // create a boolean primitives b
      boolean b;

      // assign isDefined result of ch to b
      b = Character.isDefined(ch);

      String str = "Character " +ch+ " has defined meaning in Unicode is " +b;

      // print b value
      System.out.println( str );
   }
}

Output

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

Character @ has defined meaning in Unicode is true

Example

The following example shows the usage of Java Character isDefined(int codePoint) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
      
      // create a int primitive cp
      int cp;

      // assign values to cp
      cp = 0x012345;

      // create a boolean primitives b
      boolean b;

      // assign isDefined result of cp to b
      b = Character.isDefined(cp);

      String str = "cp has defined meaning in Unicode is " + b;

      // print b value
      System.out.println( str );
   }
}

Output

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

cp has defined meaning in Unicode is true

Example

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

import java.lang.*;
public  class Demo {
   public static void main (String args[]) {
      Character c1 = new Character('>');
      if(Character.isDefined(c1))
         System.out.println("The character is defined in Unicode");
      else
         System.out.println("The character is not defined in Unicode");
   }
}

Output

The output of the code above is −

The character is defined in Unicode

Example

In the following example, let us apply conditional statements (if – else) on multiple conditions using logical operators −

import java.lang.*;
public  class Demo {
   public static void main (String args[]) {
      Character c1 = new Character('a');
      int c2 = 00123;
      if(Character.isDefined(c1) && Character.isDefined(c2))
         System.out.println("The characters c1 and c2 are defined in Unicode");
      else if(Character.isDefined(c1) && !Character.isDefined(c2))
         System.out.println("The character c1 is defined in Unicode");
      else if(!Character.isDefined(c1) && Character.isDefined(c2))
         System.out.println("The character c2 is defined in Unicode");
      else
         System.out.println("The character is not defined in Unicode");
   }
}

Output

On execution, the output of the program above is obtained as follows −

The characters c1 and c2 are defined in Unicode
java_lang_character.htm
Advertisements