Introduction
In this lab, we will learn about the isJavaIdentifierStart(int codePoint)
method in Java. This method is used to determine whether the specified Unicode codepoint character is the first character in a Java identifier or not.
In this lab, we will learn about the isJavaIdentifierStart(int codePoint)
method in Java. This method is used to determine whether the specified Unicode codepoint character is the first character in a Java identifier or not.
We will create a Java file named "CharacterExample.java" in the ~/project
directory.
cd ~/project
touch CharacterExample.java
In this step, we will import the Character
class to use the isJavaIdentifierStart(int codePoint)
method.
import java.lang.Character;
isJavaIdentifierStart(int codePoint)
methodNow, we will use the isJavaIdentifierStart(int codePoint)
method to check whether the given Unicode codepoint character is the first character of a Java identifier or not.
public class CharacterExample {
public static void main(String[] args) {
int codePoint1 = 48;
int codePoint2 = 90;
int codePoint3 = 1234;
boolean isJavaId1 = Character.isJavaIdentifierStart(codePoint1);
boolean isJavaId2 = Character.isJavaIdentifierStart(codePoint2);
boolean isJavaId3 = Character.isJavaIdentifierStart(codePoint3);
System.out.println((char)codePoint1 + " is a part of Java start identifier? : " + isJavaId1);
System.out.println((char)codePoint2 + " is a part of Java start identifier? : " + isJavaId2);
System.out.println((char)codePoint3 + " is a part of Java start identifier? : " + isJavaId3);
}
}
The isJavaIdentifierStart
method returns a boolean value. In this example, it will return true
for the code point of "Z" and "ą¶" but false for the code point of "0".
Let's compile and run the program using the following command:
javac CharacterExample.java && java CharacterExample
The output for the above program will be:
0 is a part of Java start identifier? : false
Z is a part of Java start identifier? : true
ą¶ is a part of Java start identifier? : true
Now we will create a user input example to demonstrate the use of the isJavaIdentifierStart()
method.
import java.util.Scanner;
public class CharacterExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter a Unicode codepoint: ");
int codePoint = scanner.nextInt();
boolean isJavaId = Character.isJavaIdentifierStart(codePoint);
System.out.println((char)codePoint + " is a part of Java start identifier? : " + isJavaId);
} catch(Exception e) {
System.out.println("Invalid Input!!");
}
}
}
Let's compile and run the program using the following command:
javac CharacterExample.java && java CharacterExample
The output for the above program will be:
Enter a Unicode codepoint: 101
e is a part of Java start identifier? : true
In this lab, we learned about the isJavaIdentifierStart(int codePoint)
method in the Character
class in Java. This method is used to determine whether the specified Unicode codepoint character is the first character in a Java identifier or not. We also saw how to use this method in both hardcoded and user input examples.