Java Reference
In-Depth Information
• If the value is false , the execution behavior depends on whether Expression2 is
present:
♦ If Expression2 is present, it is evaluated.
If the evaluation completes abruptly for some reason, the assert statement
completes abruptly for the same reason.
If the evaluation completes normally, an AssertionError instance whose
“detail message” is the resulting value of Expression2 is created.
If the instance creation completes abruptly for some reason, the assert
statement completes abruptly for the same reason.
If the instance creation completes normally, the assert statement com-
pletes abruptly by throwing the newly created AssertionError object.
♦ If Expression2 is not present, an AssertionError instance with no “detail mes-
sage” is created.
If the instance creation completes abruptly for some reason, the assert
statement completes abruptly for the same reason.
If the instance creation completes normally, the assert statement com-
pletes abruptly by throwing the newly created AssertionError object.
For example, after unmarshalling all of the arguments from a data buffer, a
programmer might assert that the number of bytes of data remaining in the buf-
fer is zero. By verifying that the boolean expression is indeed true , the system
corroborates the programmer's knowledge of the program and increases one's
confidence that the program is free of bugs.
Typically, assertion-checking is enabled during program development and test-
ing, and disabled for deployment, to improve performance.
Because assertions may be disabled, programs must not assume that the ex-
pressions contained in assertions will be evaluated. Thus, these boolean ex-
pressions should generally be free of side effects.
Evaluating such a boolean expression should not affect any state that is visible
after the evaluation is complete. It is not illegal for a boolean expression con-
tained in an assertion to have a side effect, but it is generally inappropriate, as
it could cause program behavior to vary depending on whether assertions were
enabled or disabled.
Along similar lines, assertions should not be used for argument-checking in
public methods. Argument-checking is typically part of the contract of a meth-
Search WWH ::




Custom Search