Java Reference
In-Depth Information
and causes the statement to be executed if the test is true. For the case where
a series of statements are needed after an if test, then they should be enclosed
in braces as in
if (test)
{
statement 1;
statement 2;
...
}
where all the statements within the braces will execute if a is less than b . Note that
a semi-colon is not necessary after the final brace. For these compound statements
we follow the common practice of putting the first brace after the control part of
the statement as in
if (test) {
statement 1;
statement 2;
...
}
We discuss below several important kinds of statements: declaration, conditional,
and flow control.
2.9.1 Declarations
A declaration gives both an identifier and a data type to a variable and provides
memory space for it. Java is a strongly typed language so every variable must be
explicitly declared a particular type. An initializer in the declaration can assign
avalue to the variable:
int x;
int x, y, z; // multiple declaration
double y = 1.0; // Declaration and initializer
double x = 5.0;
Local variables in methods must be assigned a specific value either in the declara-
tion, or afterwards, before they are used, else the compiler complains. Class and
instance variables (see Chapter 3) will be given default values by the compiler:
zero for numerical types, false for boolean , empty (zero bits) for char , and
null for object reference types.
2.9.2 Conditional statements
The conditional if statement evaluates a boolean expression to decide whether
to execute a statement, as in
if (test) statement;
Search WWH ::




Custom Search