Java Reference
In-Depth Information
• If the value is
true
, then the first contained
Statement
(the one before the
else
keyword) is executed; the
if-then-else
statement completes normally if and only if
execution of that statement completes normally.
• If the value is
false
, then the second contained
Statement
(the one after the
else
keyword) is executed; the
if-then-else
statement completes normally if and only if
execution of that statement completes normally.
14.10. The
assert
Statement
An
assertion
is an
assert
statement containing a boolean expression. An assertion is either
enabled
or
disabled
. If the assertion is enabled, execution of the assertion causes evaluation
of the boolean expression and an error is reported if the expression evaluates to
false
. If the
assertion is disabled, execution of the assertion has no effect whatsoever.
AssertStatement:
assert
Expression1
;
assert
Expression1
:
Expression2
;
It is a compile-time error if
Expression1
does not have type
boolean
or
Boolean
.
In the second form of the
assert
statement, it is a compile-time error if
Expression2
is
void
An
assert
statement that is executed after its class has completed initialization is enabled if
and only if the host system has determined that the top level class that lexically contains
the
assert
statement enables assertions.
Whether or not a top level class enables assertions is determined no later than the earliest
of the initialization of the top level class and the initialization of any class nested in the top
level class, and cannot be changed after it has been determined.
An
assert
statement that is executed before its class has completed initialization is enabled.
This rule is motivated by a case that demands special treatment. Recall that the asser-
tion status of a class is set no later than the time it is initialized. It is possible, though
generally not desirable, to execute methods or constructors prior to initialization. This
can happen when a class hierarchy contains a circularity in its static initialization, as
in the following example:
public class Foo {
public static void main(String[] args) {