Identifying Java Identifier Start Characters

JavaJavaBeginner
Practice Now

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.


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/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") 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/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-117529{{"`Identifying Java Identifier Start Characters`"}} java/classes_objects -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/class_methods -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/exceptions -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/modifiers -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/oop -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/packages_api -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/user_input -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/wrapper_classes -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/identifier -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/arrays -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/data_types -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/operators -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/output -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/strings -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/type_casting -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/variables -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} java/system_methods -.-> lab-117529{{"`Identifying Java Identifier Start Characters`"}} end

Creating a Java file

We will create a Java file named "CharacterExample.java" in the ~/project directory.

cd ~/project
touch CharacterExample.java

Importing the Character class

In this step, we will import the Character class to use the isJavaIdentifierStart(int codePoint) method.

import java.lang.Character;

Using the isJavaIdentifierStart(int codePoint) method

Now, 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".

Compiling and Running the program

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

User Input Example

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

Summary

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.

Other Java Tutorials you may like