next up previous
Next: Assignment Up: Unit 02 Previous: Variables and shoe-boxes

Variable declarations

Variables are introduced in a program through variable declarations.


Variable declaration

Syntax:

type variableName;

Semantics:

The declaration of a variable reserves a memory location for the variable and makes the variable available in the part of the program (block) where the declaration appears (more details in Unit 3). Notice that we have to declare a variable before we can use it.

Example:

String line;

After such a declaration, the variable line can be used in the program block in which the declaration appears (e.g., in the main() method, if the declaration appears there).


We can also declare several variables of the same type with a single declaration.

type variableName-1,...,variableName-n;

Such a declaration is equivalent to:

type variableName-1;
 ...
type variableName-n;


next up previous
Next: Assignment Up: Unit 02 Previous: Variables and shoe-boxes