Java Character isJavaIdentifierPart(char ch) Method

Java isJavaIdentifierPart(char ch) is a part of Character class. This method is used to determine whether the specified character is a part of a Java identifier or not.

It must be noted that this method does not handle supplementary characters. A character is a part of Java identifier if any of the following conditions are satisfied:

  • 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
  • isIdentifierIgnorable returns true for the character.

Syntax:

public static boolean isJavaIdentifierPart(char ch)

Parameters:

The parameter passed is the character to be checked for Java identifier.

Returns:

Returns the boolean value true if the specified character is a part of Java identifier else returns false.

Example 1:

Here, the characters are checked whether they are a part of a Java identifier or not.

public class StudyTonight
{  
	public static void main(String[] args)
	{  
		char ch1 = ':';  
		char ch2 = 'D';  
		char ch3 = '$';  
		char ch4  = '_';   
		char ch5  = '%';  

		boolean b1 = Character.isJavaIdentifierPart(ch1);  
		boolean b2 = Character.isJavaIdentifierPart(ch2);  
		boolean b3 = Character.isJavaIdentifierPart(ch3);  
		boolean b4 = Character.isJavaIdentifierPart(ch4);  
		boolean b5 = Character.isJavaIdentifierPart(ch5);  

		System.out.println(ch1 +" is a part of Java identifier??:  "+b1);  
		System.out.println(ch2 +" is a part of Java identifier??:  "+b2);  
		System.out.println(ch3 +" is a part of Java identifier??:  "+b3);  
		System.out.println(ch4 +" is a part of Java identifier?? : "+b4);  
		System.out.println(ch5 +" is a part of Java identifier??:  "+b5);  
	}  
} 


: is a part of Java identifier??: false
D is a part of Java identifier??: true
$ is a part of Java identifier??: true
_ is a part of Java identifier?? : true
% is a part of Java identifier??: false

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner; 
public class StudyTonight
{  
	public static void main(String[] args)
	{  
		try
		{
			System.out.print("Enter the character: ");  
			Scanner sc = new Scanner(System.in);         
			char ch = sc.next().charAt(0);  
			boolean b = Character.isJavaIdentifierPart(ch);
			System.out.println(ch + " is a part of Java identifier??: "+b);
		}
		catch(Exception e)
		{
			System.out.println("Invalid Input!!");
		}
	}  
}


Enter the character: "
" is a part of Java identifier??: false
*******************************************
Enter the character: $
$ is a part of Java identifier??: true
*******************************************
Enter the character: .
. is a part of Java identifier??: false

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.