Java OCAJP7: variables name

In Java a variable identifier must follow the following rules:

  • Must start with a letter, a ‘$’ or an ‘_’
  • Cannot start with a number
  • After the first characters can appear letters, numbers, underscores or ‘$’

The following snippet gives an edge case of variable naming:

public class JavaCertification  {
	public static void main(String[] args) {  
		int _ = 20;   // '_' is a valid identifier

This program compiles and produces the following result:


So, ‘_’ is a valid name for an identifier and it doesn’t produce a compilation error. Despite that it is better never to call a variable in this way :)

This entry was posted in $1$s. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *