Introduction
The Java isUnicodeIdentifierStart(int codePoint) method is a part of the Character class. This method is used to validate whether a specified Unicode codepoint character is allowed as the first character in a Unicode identifier or not.
Create a Java file
Create a Java file with the name UnicodeIdentifier.java in the ~/project directory. You can use any text editor or run the following command on the terminal to create the file:
touch ~/project/UnicodeIdentifier.java
Add the code for checking if a character can begin a Unicode identifier
Add the following code to define the checkUnicodeIdentifier() method that will check if a character is allowed as the first character of a Unicode identifier or not:
public class UnicodeIdentifier {
public static void checkUnicodeIdentifier(int codePoint) {
boolean isStartChar = Character.isUnicodeIdentifierStart(codePoint);
System.out.println((char)codePoint + " is a start Unicode identifier? " + isStartChar);
}
}
This method takes an integer codePoint parameter representing the Unicode codepoint of the character you want to check. It returns a boolean value of true if the specified Unicode codepoint character is allowed as the first character of a Unicode identifier, otherwise returns false.
Note: Since the method is defined in the public class, it can be accessed by any other class.
Demonstrate the usage of isUnicodeIdentifierStart(int codePoint)
Now, create a main() method to call the checkUnicodeIdentifier() with different characters to check whether they are allowed as the first character of Unicode identifier or not:
public class UnicodeIdentifier {
public static void checkUnicodeIdentifier(int codePoint) {
boolean isStartChar = Character.isUnicodeIdentifierStart(codePoint);
System.out.println((char)codePoint + " is a start Unicode identifier? " + isStartChar);
}
public static void main(String[] args) {
// Check if 'C' is a valid start character of a Unicode identifier
checkUnicodeIdentifier(67);
// Check if '1' is a valid start character of a Unicode identifier
checkUnicodeIdentifier(49);
}
}
In the code above, we have called the checkUnicodeIdentifier() method with two different codepoints. The first call has a codepoint value of 67 which represents the letter 'C' and the second call has a codepoint value of 49 which represents the number '1'.
Compile and run the program
Compile the program using the following command:
javac UnicodeIdentifier.java
Run the program using the following command:
java UnicodeIdentifier
Analyze the output
After running the program, the output should be as follows:
C is a start Unicode identifier? true
1 is a start Unicode identifier? false
In the main() method, we have called the checkUnicodeIdentifier() method with different codepoints. The first call has a codepoint value of 67 which represents the letter 'C' and it returns true since 'C' is a valid start character of a Unicode identifier. The second call has a codepoint value of 49 which represents the number '1' and it returns false since '1' is not a valid start character of a Unicode identifier.
Summary
This lab has introduced you to the Java isUnicodeIdentifierStart(int codePoint) method. You have learned how to use this method to check if a Unicode codepoint character is allowed as the first character of a Unicode identifier. You also have learned how to create a Java file, define and call methods, compile and run the Java program in the terminal.



