I will explain about the assertions briefly here. If you need more about this you can refer to any java books or websites.
An assertion is a statement in java containing a boolean expression that is assumed to be true when the statement is executed. The system reports an "AssertionError" if the expression evaluates to false. It is used for debugging purposes:
assert(a > 0); // throws an AssertionError if a <= 0
Assertions can be in two forms:
assert Expression1 ;
assert Expression1 : Expression2 ;
Expression1 should always result in a boolean value.
Expression2 can be anything that results in a value. The value is used to generate a String message that displays more debugging information.
int a ;
public void func()
assert (a > 0): "a is negative" ;
// Remaining code
In the above code, the assertion expression is true if a is positive. If a is negative or zero, an AssertionError is thrown with the message "a is negative."
Assertions are disabled by default. To compile with assertions enabled, you need to use the source 1.4 flag:
javac -source 1.4 AssertionTest.java
To enable assertions at runtime, use the -enableassertions or -ea flags.
For selective disabling of assertions at runtime, use the -da or -disableassertions flags.
For enabling assertions in the system classes, use the -esa or -dsa flags. Assertions can also be enabled or disabled on a package basis.
java -ea AssertionTest
Appropriate and inappropriate use of assertions:
You can place an assertion at any location that you don't expect to be reached normally. Assertions can be used to validate the parameters passed to a private method. However, assertions should not be used to validate parameters passed to public methods because a public method must check its arguments regardless of whether assertions are enabled or not. However, you can test postconditions with assertions in both public and non-public methods. Also, assertions should not change the state of a program in any manner.
Hope you understand and let me know if you have any questions.