Java - Character isJavaIdentifierStart() Method



The Java Character isJavaIdentifierStart() method determines if the specified character is permissible as the first character in a Java identifier. As we already know, a Java Identifier is a sequence of characters in which the first character is valid only if it is a letter (a-z, A-Z), $ or an underscore (_).

A character may start a Java identifier if and only if one of the following conditions is true −

  • isLetter(ch) returns true

  • getType(ch) returns LETTER_NUMBER

  • ch is a currency symbol (such as '$')

  • ch is a connecting punctuation character (such as '_').

Syntax

Following is the declaration for Java Character isJavaIdentifierStart() method

public static boolean isJavaIdentifierStart(char ch)
(or)
public static boolean isJavaIdentifierStart(int codePoint)

Parameters

  • ch − the character to be tested

  • codePoint − the Unicode code point to be tested

Return Value

This method returns true if the character may start a Java identifier, otherwise false.

Example

The following example shows the usage of Java Character isJavaIdentifierStart(char ch) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create a char primitive ch
      char ch;

      // assign values to ch
      ch = '3';

      // create a boolean primitive b
      boolean b;

      /**
       *  check if ch can start a java identifier
       *  and assign result to b
       */
      b = Character.isJavaIdentifierStart(ch);
      String str = ch + " may start a Java identifier is " + b;
      
      // print b value
      System.out.println( str );
      
   }
}

Output

Let us compile and run the above program, this will produce the following result −

3 may start a Java identifier is false

Example

The following example shows the usage of Java Character isJavaIdentifierStart(int codePoint) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x0034;
      cp2 = 0x004a;

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      /**
       *  check if characters represented by cp1, cp2 can start a
       *  java identifier and assign results to b1, b2
       */
      b1 = Character.isJavaIdentifierStart(cp1);
      b2 = Character.isJavaIdentifierStart(cp2);
      String str1 = "cp1 may start a Java identifier is " + b1;
      String str2 = "cp2 may start a Java identifier is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

cp1 may start a Java identifier is false
cp2 may start a Java identifier is true

Example

Another example to determine whether the character is a valid Java identifier start using the method is given below. Here, we are passing symbols as its argument.

import java.lang.*;
public class IdentifierDemo {
   public static void main(String []args) {
      boolean b1 = Character.isJavaIdentifierStart('$');
      boolean b2 = Character.isJavaIdentifierStart('&');
      System.out.println(b1);
      System.out.println(b2);
   }
}

Output

Let us compile and run the program above and display the output obtained below −

true
false

Example

All letters return true when they are passed as arguments to this method. The example program is given below.

import java.lang.*;
public class IdentifierDemo {
   public static void main(String []args) {
      boolean b1 = Character.isJavaIdentifierStart('a');
      boolean b2 = Character.isJavaIdentifierStart('X');
      System.out.println(b1);
      System.out.println(b2);
   }
}

Output

Let us compile and run the program above and display the output obtained below −

true
true

Example

In another scenario, the method checks whether the argument is a starting Java identifier or not and displays the return values obtained by using conditional statements if – else

import java.lang.*;
public class IdentifierDemo {
   public static void main(String []args) {
      char ch = '$';
      boolean b = Character.isJavaIdentifierStart(ch);
      if(b == true)
         System.out.println("The character is a java identifier");
      else
         System.out.println("The character is not a java identifier");
   }
}

Output

The program above must be compiled and run before the output is displayed as follows −

The character is a java identifier
java_lang_character.htm
Advertisements