Java - Character isUnicodeIdentifierPart() Method



The Java Character isUnicodeIdentifierStart() method determines if the specified character or a code point is permissible as the first character in a Unicode identifier.

A character may start a Unicode identifier if and only if one of the following conditions is true −

  • isLetter(ch) returns true.

  • getType(ch) returns LETTER_NUMBER.

It suggests that the Unicode identifier always starts with either letter or a digit only.

This method occurs in two polymorphic forms with a same return type and different parameter types.

Syntax

Following is the syntax for Java Character isUnicodeIdentifierStart() method

public static boolean isUnicodeIdentifierStart(char ch)
(or)
public static boolean isUnicodeIdentifierStart(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 argument may start a Unicode identifier, false otherwise.

Example

The following example shows the usage of Java Character isUnicodeIdentifierStart(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 = 'p';

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

      /**
       *  check if ch1, ch2 may start a Unicode identifier 
       *  and assign results to b1, b2
       */
      b1 = Character.isUnicodeIdentifierStart(ch1);
      b2 = Character.isUnicodeIdentifierStart(ch2);
      String str1 = ch1 + " may start a Unicode identifier is " + b1;
      String str2 = ch2 + " may start 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 start a Unicode identifier is false
p may start a Unicode identifier is true

Example

The following example shows the usage of Java Character isUnicodeIdentifierStart(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 = 0x037e; // represents GREEK QUESTION MARK
      cp2 = 0x05d1; // represents HEBREW LETTER BET

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

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

Example

As mentioned earlier, this method will return true if the isLetter() method returns true. Let us see an example demonstrating it.

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

Output

Once we compile and run the program, the output is displayed as follows −

---------First Case-----------
false
false
---------Another Case-----------
true
true

Example

Similarly, the isUnicodeIdentifierStart() method will return true if the return value of the getType() method is LETTER_NUMBER.

import java.lang.*;
public class UnicodeIdentifierDemo {
   public static void main(String []args){
      char c = '8';
      System.out.println(Character.getType(c)); //returns a number type
      boolean b = Character.isUnicodeIdentifierStart(c); //returns true
      System.out.println(b);
   }
}

Output

The output of the program after being compiled and run will be printed as follows −

9
true
java_lang_character.htm
Advertisements