SE450: Assertions: The assert functionality in Java 1.4 [4/24] Previous pageContentsNext page

Definition of an assertion: A statement containing a boolean expression that the programmer believes to be true at the time the statement is executed.

What does assert do?

What do you use an assertion for?

Syntax
AssertStatement:
assert Expression1;
assert Expression1 : Expression2 ;
Expression2 is like passing a String expression to System.out.println();

Examples:


assert i == 0;

assert b.getName() != null;

assert false;  // will always throw an AssertionError if assertions are enabled

assert i == 0 : "i != 0, loop failed"; // will pass message out in error

Previous pageContentsNext page