DEFINITE ASSIGNMENT
Definite Assignment
16
void flow(boolean flag) {
int k;
if (flag)
k = 3;
if (!flag)
k = 4;
System.out.println(k); // k
is not definitely assigned before here
}
and so compiling this program must cause a compile time error to occur.
In order to precisely specify all the cases of definite assignment, the rules in
this section define two technical terms:
whether a local variable is
definitely assigned before
a statement or expres
sion, and
whether a local variable is
definitely assigned after
a statement or expression.
In order to specify boolean valued expressions, the latter notion is refined into
two cases:
whether a local variable is
definitely assigned after
the expression
when true
,
and
whether a local variable is
definitely assigned after
the expression
when false
.
Here
when true
and
when false
refer to the value of the expression. For example,
the local variable k is definitely assigned a value after evaluation of the expression
a && ((k=m) > 5)
when the expression is
true
but not when the expression is
false
(because if
a
is
false
, then the assignment to
k
is not executed (more properly, evaluated)).
The statement
V
is definitely assigned after
X
(where
V
is a local variable
and
X
is a statement or expression) means
V
is definitely assigned after
X
if
X
completes normally . If
X
completes abruptly, the assignment may not have
occurred, and the rules stated here take this into account. A peculiar consequence
of this definition is that
V
is definitely assigned after
break;
is always true!
Because a
break
statement never completes normally, it is vacuously true that
V
has been assigned a value if the
break
statement completes normally.
To shorten the rules, the customary abbreviation iff is used to mean if and
only if .
Let
V
be a local variable. Let
a
,
b
,
c
, and
e
be expressions. Let
S
and
T
be
statements.
385
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved