Java - Character isJavaIdentifierPart() Method



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

A character may be part of a Java identifier if any of the following are true −

  • It is a letter

  • It is a currency symbol (such as '$')

  • 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

Note − The method occurs in two polymorphic forms with same return type but different parameter types.

Syntax

Following is the syntax for Java Character isJavaIdentifierPart() method

public static boolean isJavaIdentifierPart(char ch)
(or)
public static boolean isJavaIdentifierPart(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 Java identifier, otherwise false.

Example

The following example shows the usage of Java Character isJavaIdentifierPart(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 = '3';
      ch2 = '/';

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

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

3 may be part of a Java identifier is true
/ may be part of a Java identifier is false

Example

The following example shows the usage of Java Character isJavaIdentifierPart(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 = 0x01f1;
      cp2 = 0x07c0;

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

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

Example

In this example program, we will declare a character array and initialize it with length 7; we then use loop statements (for loop) to invoke the method by passing each element of the character array as its argument. Using conditional statements, the element is checked whether it is a part of the Java Identifiers or not.

import java.lang.*;
public class Main {
   public static void main(String[] args) {
      char ch[] = new char[] {'a', '3', '/', '~', 'X', '_', '$'};
      int count = 0;
      for(int i = 0; i < ch.length; i++){
         if(Character.isJavaIdentifierPart(ch[i])){
            System.out.println("The character is a part of Java Identifiers");
            count++;
         }
         else
            System.out.println("The character is not a part of Java Identifiers");
      }
      if(count == 0)
         System.out.println("Java Identifiers do not exist in this array");
   }
}

Output

The program given above is compiled and run, and then the output is displayed as follows −

The character is a part of Java Identifiers
The character is a part of Java Identifiers
The character is not a part of Java Identifiers
The character is not a part of Java Identifiers
The character is a part of Java Identifiers
The character is a part of Java Identifiers
The character is a part of Java Identifiers

Example

In this example program, we will just use the conditional statements without the loop statements. Therefore, only a char variable is initialized to be passed as an argument.

import java.lang.*;
public class Main {
   public static void main(String[] args) {
      char ch = '%';
      if(Character.isJavaIdentifierPart(ch))
         System.out.println("The character is a part of Java Identifiers");
      else
         System.out.println("The character is not a part of Java Identifiers");
   }
}

Output

On compiling and running the program above, the output is printed as follows −

The character is not a part of Java Identifiers

Example

Lastly, let us look at some general errors this method might raise –

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      char c = '\u0026';
      System.out.println((int)Character.isJavaIdentifierPart(c));
   }
}

Compile Time Error

Boolean data types cannot be type casted in Java −

Demo.java:6: error: incompatible types: boolean cannot be converted to int
    System.out.println((int)Character.isJavaIdentifierPart(c));
                                                          ^
1 error
java_lang_character.htm
Advertisements