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.

Checking If a char is a Permissible Java identifier as First char Example

The following example shows the usage of Java Character isJavaIdentifierStart(char ch) method. In this program, we've created a char variable and assigned it a value. Now using isJavaIdentifierStart() method, we're checking if char variable is a permissible Java Identifier First char or not and result is printed.

package com.tutorialspoint;

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

Checking If a codepoint is a Permissible Java identifier as First char Example

The following example shows the usage of Java Character isJavaIdentifierStart(int codepoint) method. In this program, we've created two int variables and assigned them few values. Now using isJavaIdentifierStart() method, we're checking if int variable is a permissible Java Identifier First char or not and result is printed.

package com.tutorialspoint;

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

Checking If a char is a Permissible Java identifier as First char 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.

package com.tutorialspoint;

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

Checking If a char is a Permissible Java identifier as First char Example

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

package com.tutorialspoint;

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

Checking If a char is a Permissible Java identifier as First char 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

package com.tutorialspoint;

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