Java Keywords

The Java reserved words are listed below. Some of the keywords have no current meaning and are reserved for future expansion.

break switch instanceof byte public

continue catch interface char static

do finally new double cast

for throw null float future

return throws super int generic

while try this long goto

case abstract import const inner

default class package final native

else extends synchronize private operator

if implements Boolean protected outer

rest transient var volatile short

void

Literals

Literals are the representation of values.

\n new line \ r carriage return

\ t tab \ " double quote

\ ‘ single quote \ \ backslash

 

 

Comments

There are two regular comment styles, and a special comment style used to generate class library documentation.

// This is a comment

/ * This is a comment * /

/ * * This is a special comment for documentation * * /

 

 

 

Rules in naming variables

Variables (identifiers) are names for classes, methods, variables and blocks. As in all programming languages, Java variables adhere to some rules, namely:

 

There are 3 kinds of variables in Java:

Instance variables

class VariableDemo {

int num1, num2 ;

double sum;

public void add ()

sum = num1 + num2;

}

public void paint (Graphics g ) {

g . drawString ("The sum is "+sum, 35, 100 ) ;

}

}

In the example, variables num1 and num2 of type integer and sum of type double are declared as instance variables. These variables, therefore, can be accessed anywhere in the program (inside or outside a method). [Instance Variables, 5 of 8]

 

Class variables

class VariableDemo {

Static int [] = {1, 2, 3, 4, 5, 6, 7 } ;

. . .

}

The array i which is an array of integers is declared as a class variable with a static value of{1,2,3,4,5,6,7}. Array i, being a class variable, is therefore accessible to the class and all its instances. [Class Variable, 6 of 8]

 

Local variables

class VariableDemo {

String str ;

public void displayString {

boolean more ;

if (more) . . .

. . .

}

The variable more which of type boolean is local to method displayString. Once displayString finishes its execution, the variable more does not exist and may not be used anywhere in the program. [Local Variables, 7 of 8]

 

Constants

Constants refers to those whose value does not change. In Java, the keyword final is used to refer to a constant.

class VariableDemo {

double charge1, charge2 ;

public void computeTemp () {

final double rate = 3.5 ;

. . .

}

}

The constant rate, which is of type double, has a constant value of 3.5. This value is retained throughout the course of the program. [Constants, 8 of 8]