Java - Character isUnicodeIdentifierPart() Method



The Java Character isUnicodeIdentifierPart() method determines if the specified character may be part of a Unicode identifier as other than the first character.

A character may be part of a Unicode identifier if and only if one of the following statements is true −

  • It is a letter

  • It is a connecting punctuation character (such as '_')

  • It is a digit

  • It is a numeric letter (such as a Roman numeral character)

  • It is a combining mark

  • It is a non-spacing mark

  • isIdentifierIgnorable returns true for this character.

This method occurs in two polymorphic forms with same return type but different parameters.

Syntax

Following is the syntax for Java Character isUnicodeIdentifierPart() method

public static boolean isUnicodeIdentifierPart(char ch)
(or)
public static boolean isUnicodeIdentifierPart(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 may be part of a Unicode identifier, false otherwise.

Example

The following example shows the usage of Java Character isUnicodeIdentifierPart(char ch) 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 = '~';
      ch2 = '1';

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

      /**
       *  check if ch1, ch2 may be part of a Unicode identifier
       *  and assign results to b1, b2.
       */
      b1 = Character.isUnicodeIdentifierPart(ch1);
      b2 = Character.isUnicodeIdentifierPart(ch2);
      String str1 = ch1 + " may be part of a Unicode identifier is " + b1;
      String str2 = ch2 + " may be part of a Unicode identifier 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 −

~ may be part of a Unicode identifier is false
1 may be part of a Unicode identifier is true

Example

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

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

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

      // assign values to cp1, cp2
      cp1 = 0x053e; // represents ARMENIAN CAPITAL LETTER CA
      cp2 = 0x0040; // represents @

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

      /**
       *  check if cp1, cp2 may be part of a Unicode identifier
       *  and assign results to b1, b2.
       */
      b1 = Character.isUnicodeIdentifierPart(cp1);
      b2 = Character.isUnicodeIdentifierPart(cp2);
      String str1 = "cp1 may be part of a Unicode identifier is " + b1;
      String str2 = "cp2 may be part of a Unicode identifier 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 may be part of a Unicode identifier is true
cp2 may be part of a Unicode identifier is false

Example

This example checks whether the roman numerals passed as the arguments to the method are Unicode Identifier part or not.

import java.lang.*;
public class UnicodeIdentifierDemo{
   public static void main(String []args){
      char c1 = '\u2167'; // code point of roman numeral 8
      boolean b1 = Character.isUnicodeIdentifierPart(c1);
      System.out.println(b1);
   }
}

Output

The output of the program above is given as −

true

Example

The method also returns true if the isIdentifierIgnorable() method returns true for the input character. Let us see an example to prove that −

import java.lang.*;
public class UnicodeIdentifierDemo {
   public static void main(String []args){
      char c1 = '\u0000';
      boolean b1 = Character.isIdentifierIgnorable(c1);
      boolean b2 = Character.isUnicodeIdentifierPart(c1);
      System.out.println(b1);
      System.out.println(b2);
      System.out.println("---------Another Case-----------");
      char c2 = 'a';
      boolean b3 = Character.isIdentifierIgnorable(c2);
      boolean b4 = Character.isUnicodeIdentifierPart(c2);
      System.out.println(b3);
      System.out.println(b4);
   }
}

Output

In the program above, it is not necessary that for all true return values of the Character.isUnicodeIdentifierPart() method, the return values of the Character.isIdentifierIgnorable() method will also be true. But for all true return values of the Character.isIdentifierIgnorable() method, the return values of the Character.isUnicodeIdentifierPart() method will also be true.

The output is as follows:

true
true
---------Another Case-----------
false
true
java_lang_character.htm
Advertisements