An assertion is for testing your assumption in your program. (introduced in JDK 1.4)
You can enable or disable assertions.
While the unnecessary ‘try – catch’ block needs to be removed by hand after completion of creating a program, assertions will easily be enabled or disabled at any time.
In JDK 1.4, assertions are disabled by default, and thus assertions are ignored when you execute a program that has assertions.
Enable assertions
java -ea
java -enableassertions
Disable assertions
java -da
java -disableassertions
Enable assertions in the test.Test class
java ea:test.Test
Enable assertions except ones in the System class
java -ea -dsa
Enable assertions except ones in the test package
java -ea -da:test
How to write assertions
int x = 2 + 2;
assert (x == 4); // If it is not true an AssertionError is thrown