Java Character isJavaIdentifierPart() Example

Many methods exist in java.lang.Character class used for different purposes. Some methods are useful for checking the character is a digit or letter or is a uppercase letter or lowercase letter etc. At the sametime, some methods can modify the case of the letters from uppercase to lowercase or vice versa.

Few more methods exist whether the character is fit to be the first character in a Java identifier like isJavaIdentifierStart(). Another method exist, isJavaIdentifierPart() to know whether character can be a part of Java identifier. We know a Java identifier should start with a letter, a digit or an underscore. Except the first character, the remaining characters can be a combination of digits, letters, $ symbol, underscore etc., but not special characters and spaces. isJavaIdentifierPart(char ch) returns true for all fitting characters and returns false for special characters and spaces.

Note: isJavaIdentifierPart(char ch) is very different from isJavaIdentifierStart(char ch).

Following is the method signature as defined in java.lang.Character class.

  • public static boolean isJavaIdentifierPart(char ch): Checks the specified character ch is permitted to be a part Java identifier. Returns a boolean value of true if suits, else false. Introduced with JDK 1.5.
Following example on Java Character isJavaIdentifierPart() Example illustrates. Just for interest, special characters are also checked.
public class CharacterFunctionDemo
  public static void main(String args[])
   char ch1 = 'a';
   char ch2 = '*';                      

   boolean b1 = Character.isJavaIdentifierPart(ch1);
   boolean b2 = Character.isJavaIdentifierPart(ch2);

   System.out.println(ch1 + " is permitted as part of Java identifier: " + b1);     
   System.out.println(ch2 + " is permitted as part of Java identifier: " + b2);     
   System.out.println("_ is permitted as part of Java identifier: " + Character.isJavaIdentifierPart('_'));
   System.out.println("5 is permitted as part of Java identifier: " + Character.isJavaIdentifierPart('5'));
   System.out.println("+ is permitted as part of Java identifier: " + Character.isJavaIdentifierPart('+'));

Java Character isJavaIdentifierPart() Example
Output screenshot on Java Character isJavaIdentifierPart() Example

Observe, the special characters * and + returned false.

Leave a Comment

Your email address will not be published.