Java - Character isValidCodePoint() Method



Description

The Java Character isValidCodePoint() method determines whether a code point value is a valid Unicode code point value or not. The value that falls in the range U+0000 to U+10FFFF is called a valid code point of a character.

Code point in the Unicode system is defined as the numerical value (an integer data type) equivalent of a character (char data type).

They are divided into two categories − Basic Multilingual Plane (BMP) characters, and Supplementary characters. The BMP characters are ordinary characters that can be represented in 16-bit Unicode system, whereas Supplementary characters cannot be represented by 16-bit Unicode system.

BMP characters fall in the range of U+0000 to U+FFFF and supplementary characters fall in the range U+10000 to U+10FFFF.

Syntax

Following is the syntax for Java Character isValidCodePoint() method

public static boolean isValidCodePoint(int codePoint)

Parameters

  • codePoint − the Unicode code point to be tested

Return Value

This method returns true if the specified code point value is a valid code point (falls between MIN_CODE_POINT and MAX_CODE_POINT inclusive), otherwise it returns false.

Checking a CodePoint to be a valid Unicode CodePoint Example

The following example shows the usage of Java Character isValidCodePoint() method. In this program, we've created two int variables and assigned them two hexadecimal values. Then using isValidCodePoint() method, we've checked the int variables status and result is printed.

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 = 0x0123;
      cp2 = 0x123fff;

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

      /**
       *  check if cp1, cp2 are valid Unicode code points 
       *  and assign results to b1, b2
       */
      b1 = Character.isValidCodePoint(cp1);
      b2 = Character.isValidCodePoint(cp2);
      String str1 = "cp1 is a valid Unicode code point is " + b1;
      String str2 = "cp2 is a valid Unicode code point 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 is a valid Unicode code point is true
cp2 is a valid Unicode code point is false

Checking a Char to be a valid Unicode CodePoint Example

As the code point is a numerical equivalent of a char value, we can use the typecasting technique on a char variable and convert it into an int value. This int value is passed as an argument to the method.

package com.tutorialspoint;

public class CodePointDemo {
   public static void main(String args[]) {
      char ch = 's';
      boolean b = Character.isValidCodePoint((int)ch);
      System.out.println(ch + " is a valid code point: " + b);
   }
}

Output

The output is obtained as follows −

s is a valid code point: true

Checking a CodePoint to be a valid Unicode CodePoint Example

As the return type of this method is Boolean, we use conditional statements in this example to check whether the code point given as the argument to the method is a valid code point or not.

package com.tutorialspoint;

public class CodePointDemo {
   public static void main(String args[]) {
      int ch = 0x0012;
      if(Character.isValidCodePoint(ch))
         System.out.println(ch + " is a valid code point");
      else
         System.out.println(ch + " is not a valid code point");
   }
}

Output

Once the program above is compiled and run, the output will be displayed as given below −

18 is a valid code point

Checking a CodePoint to be a valid Unicode CodePoint Example

In the following example, we are passing the elements in an array of code points as arguments to the method using loop statements and check if these values are valid or not.

package com.tutorialspoint;

public class CodePointDemo {
   public static void main(String args[]) {
      int ch[] = {0x0012, 223, 0x15FF};
      for(int i = 0; i < ch.length; i++) {
         if(Character.isValidCodePoint(ch[i]))
            System.out.println(ch[i] + " is a valid code point");
         else
            System.out.println(ch[i] + " is not a valid code point");
      }
   }
}

Output

The output is obtained as follows when the program above is compiled and run −

18 is a valid code point
223 is a valid code point
5631 is a valid code point
java_lang_character.htm
Advertisements