Site Search:

Declare and initialize variables (including casting of primitive data types)

Back OCAJP


A statement declaring and initializing variables includes data type, variable name and semicolon, it can optionally includes access modifier and initial value.

The variables names, class names, methods names are all follow the rule of java identifier names. For the JCA test, we need to remember the following rule.

The only allowed characters include letters, digits, $ and _.
Digits are not allowed as the first character of the name.
The name can not be a java keywords.

All the following code compiles.



OCAJP>cat _test_.java 
class _test_ {
  public int _123, $$, Class=5; long $8;  //declare multiple variables
  String _$kr54$$_4M; 
  String Boolean = "";  //while boolean is a keyword, Boolean is not
  private void $55TP_() {}
}
OCAJP>javac _test_.java 
OCAJP>


OCAJP quiz