Java - Assert

About

The asert keyword in Java is a inline assertion.

Syntax

It's first role is to reduce the following code:

if(variable == null) {
    throw new RuntimeException("variable is null");
}

by this:

assert variable != null : "variable is null";

If the assertion fails, you get a java.lang.AssertionError:

java.lang.AssertionError: variable  is null

Other assertion example

requireNonNull

this.session = java.util.Objects.requireNonNull(session, "session is null");

Powered by ComboStrap