What are valid identifiers in Java?

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 05-Jan-2018 08:45:06
Advertisements