Asserting
suggest changeVersions
Java SE 1.4 through Java SE 9 (Early Access).
Syntax
- assert expression1;
- assert expression1 : expression2;
Parameters
Parameter | Details |
——— | —–– |
expression1 | The assertion statement throws an AssertionError if this expression evaluates to false. |
expression2 | Optional. When used, AssertionErrors thrown by the assert statement have this message. |
Remarks
By default, assertions are disabled at runtime.
To enable assertions, you must run java with -ea flag.
java -ea com.example.AssertionExample
Assertions are statements that throw an AssertionError if their expression evaluates to false. Use them for invariants you believe are always true. Do not use them for public-method argument checks or for work the program needs in order to run correctly: they are disabled unless -ea is set.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents