HOWTO · Java
The assert Keyword in Java
Learn when and how to use Java assertions, enable them with -ea, and apply them to internal assumptions.
On this page
Java’s assert keyword checks a programmer’s assumption, and the JVM disables assertions by default. Launch the program with -ea to enable them. When an enabled assertion’s condition evaluates to false, Java throws an AssertionError; otherwise, execution continues. Use assertions for internal assumptions that should hold during development, not for behavior the application must perform in every run.
Use assert as a Boolean Condition in Java
The basic form is assert condition;, where condition must be a boolean expression. If assertions are enabled and that expression is false, Java throws an AssertionError at the assertion statement. With assertions disabled, the JVM skips the assertion, including evaluation of its condition. This is why the condition must not perform work that the program depends on.
This complete class checks a count and prints a short result.
public class JavaAssertCheck {
public static void main(String[] args) {
String[] names = {"John", "Mary", "David"};
assert names.length == 3;
System.out.println("Name count: " + names.length);
}
}
Compile the class, then run it with java -ea JavaAssertCheck to enable the check. Running without -ea skips it. If you change the expected count to a value that does not match the array and enable assertions, the program stops at the assertion with an AssertionError.
Add a Diagnostic Message with assert condition : expression
Use assert condition : expression when a failure should carry context. The expression is evaluated only when assertions are enabled and the condition is false; its value becomes the detail message of the resulting AssertionError. For example, assert names.length == expected : "Expected " + expected + " names, got " + names.length; identifies both counts. Keep the expression free of side effects, since it is not evaluated on a passing condition or when assertions are disabled.
Use assert to Flag an Unexpected Branch
An assertion can mark a runtime branch that the program’s logic says should never be reached. Java rejects a statement placed after an unconditional return, so put the assertion inside a branch that the compiler cannot prove unreachable:
public class StatusLabel {
static String label(int status) {
if (status == 0) {
return "ready";
} else if (status == 1) {
return "busy";
} else {
assert false : "Unexpected status: " + status;
return "unknown";
}
}
public static void main(String[] args) {
System.out.println(label(0));
}
}
When assertions are enabled, reaching the final branch raises an AssertionError with the status as context. When they are disabled, execution reaches the fallback return, so this assertion is a diagnostic aid rather than required control flow.
Use Assertions for Internal Invariants Safely
Assertions are useful for documenting an invariant established by the program itself—for example, that a value remains within a range after an internal calculation. They can make a violated assumption easier to locate during development.
Do not use an assertion to validate arguments supplied to a public method: callers need a defined validation response regardless of JVM options. Likewise, do not put required application work inside an assertion, because disabled assertions skip its condition. Avoid side effects in assertion expressions for the same reason. Use ordinary validation and control flow whenever the program must respond consistently in production.