What are valid identifiers in Java?


A valid identifier in java –

  • Must begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
  • Can have any combination of characters after the first character.
  • Cannot be a keyword.

Example

Following example shows various possible identifiers used to declare a variable in Java.

Live Demo

public class VariableTest {
   public static void main(String args[]) {
      // Declaring a variable named num
      int num = 1;
      int _num = 10;
      int $num = 100;
      int num123 = 1000;
      int NUM = 10000;     
      //Printing the value of the variable num
      System.out.println("value if the variable num: "+num);
      System.out.println("value if the variable _num: "+_num);
      System.out.println("value if the variable $num: "+$num);
      System.out.println("value if the variable num123: "+num123);
      System.out.println("value if the variable NUM: "+NUM);
   }
}

Output

value if the variable num: 1
value if the variable _num: 10
value if the variable $num: 100
value if the variable num123: 1000
value if the variable NUM: 10000

Updated on: 30-Jul-2019

624 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements