Validating Unicode Identifier Start

JavaJavaBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/type_casting("`Type Casting`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/classes_objects -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/class_methods -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/modifiers -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/oop -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/wrapper_classes -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/identifier -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/arrays -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/comments -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/data_types -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/operators -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/output -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/strings -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/type_casting -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/variables -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} java/system_methods -.-> lab-117565{{"`Validating Unicode Identifier Start`"}} end

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.

Other Java Tutorials you may like