Java Character isJavaIdentifierPart Method

JavaJavaBeginner
Practice Now

Introduction

In Java, the Character class provides a lot of useful methods that help us to work with characters. One of these methods is isJavaIdentifierPart(int codePoint), which checks whether the specified Unicode codepoint character is a part of a Java 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/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/classes_objects -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/class_methods -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/modifiers -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/oop -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/user_input -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/wrapper_classes -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/identifier -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/arrays -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/data_types -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/for_loop -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/if_else -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/operators -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/output -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/strings -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/variables -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/string_methods -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} java/system_methods -.-> lab-117525{{"`Java Character isJavaIdentifierPart Method`"}} end

Set Up

First, create a file named Main.java in the ~/project directory using the following command:

touch ~/project/Main.java

Then, open the file using a text editor of your choice:

touch ~/project/Main.java

Check if a character is a part of a Java identifier

In this step, you will use the isJavaIdentifierPart method to check whether a given character is a part of a Java identifier or not.

Add the following code to the Main.java file:

public class Main {
    public static void main(String[] args) {
        int codePoint = 65;
        boolean isJavaIdentifierPart = Character.isJavaIdentifierPart(codePoint);

        if(isJavaIdentifierPart) {
            System.out.println("The character is a part of a Java identifier.");
        } else {
            System.out.println("The character is not a part of a Java identifier.");
        }
    }
}

The above code is checking whether the character with Unicode code point 65 is a part of a Java identifier or not. In this case, 65 corresponds to the uppercase letter 'A'.

To run the code, use the following command:

javac Main.java && java Main

User Input

In this step, you will modify the code to accept user input for the Unicode code point.

Add the following code to replace the contents of the main method in the Main.java file:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter a Unicode code point: ");
    int codePoint = scanner.nextInt();

    boolean isJavaIdentifierPart = Character.isJavaIdentifierPart(codePoint);

    if(isJavaIdentifierPart) {
        System.out.println("The character is a part of a Java identifier.");
    } else {
        System.out.println("The character is not a part of a Java identifier.");
    }

    scanner.close();
}

The above code prompts the user to enter a Unicode code point and then checks whether the corresponding character is a part of a Java identifier or not.

To run the code, use the following command:

javac Main.java && java Main

Test with different Unicode code points

In this step, you will test the code with different Unicode code points to check whether the corresponding characters are a part of a Java identifier or not.

Run the Main.java file and enter different Unicode code points to test the isJavaIdentifierPart method.

To run the code, use the following command in the terminal:

javac Main.java && java Main

Check if a character is a part of a Java identifier using a string

In this step, you will modify the code to check whether a character in a string is a part of a Java identifier or not.

Add the following code to replace the contents of the main method in the Main.java file:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter a string: ");
    String line = scanner.nextLine();

    for (int i = 0; i < line.length(); i++) {
        boolean isJavaIdentifierPart = Character.isJavaIdentifierPart(line.codePointAt(i));
        char c = line.charAt(i);

        if(isJavaIdentifierPart) {
            System.out.println("The character '" + c + "' at position " + i + " is a part of a Java identifier.");
        } else {
            System.out.println("The character '" + c + "' at position " + i + " is not a part of a Java identifier.");
        }
    }

    scanner.close();
}

The above code prompts the user to enter a string and then checks whether each character in the string is a part of a Java identifier or not.

To run the code, use the following command in the terminal:

javac Main.java && java Main

Ignore the case sensitivity

In this step, you will modify the code to ignore the case sensitivity when checking whether a character is a part of a Java identifier or not.

Add the following code to replace the contents of the main method in the Main.java file:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter a string: ");
    String line = scanner.nextLine().toLowerCase();

    for (int i = 0; i < line.length(); i++) {
        boolean isJavaIdentifierPart = Character.isJavaIdentifierPart(line.codePointAt(i));
        char c = line.charAt(i);

        if(isJavaIdentifierPart) {
            System.out.println("The character '" + c + "' at position " + i + " is a part of a Java identifier.");
        } else {
            System.out.println("The character '" + c + "' at position " + i + " is not a part of a Java identifier.");
        }
    }

    scanner.close();
}

The above code converts the user input string to lowercase and then checks whether each character in the string is a part of a Java identifier or not, ignoring the case sensitivity.

To run the code, use the following command in the terminal:

javac Main.java && java Main

Summary

In this lab, you learned how to use the isJavaIdentifierPart method to check whether a given character is a part of a Java identifier or not. You also learned how to accept user input and how to check whether each character in a string is a part of a Java identifier or not. Finally, you saw how to ignore the case sensitivity when checking whether a character is a part of a Java identifier or not.

Other Java Tutorials you may like