Java Assertions

Ads
 

Java Assertions

In Java 1.4, Assertion is a keyword that is represented by a boolean expression and enables you to test your assumptions about your program.

In Java 1.4, Assertion is a keyword that is represented by a boolean expression and enables you to test your assumptions about your program.

Java Assertions

     

In Java 1.4, Assertion is a keyword that is represented by a boolean expression and enables you to test your assumptions about your program. It evaluates to true, but if the expression is not true, the system will throw an error through an AssertionError. 

The assertion statement has two forms. The first, simpler form is:

assert Expression1 ;
where Expression1 is a boolean expression. When the system runs the assertion, it evaluates Expression1 and if it is false throws an AssertionError with no detail message.

The second form of the assertion statement is:

assert Expression1 : Expression2 ;where:
Expression1 is a boolean expression.
In the second form, expression2 provides a means of passing a String message to the assertion facility. If the condition evaluates to false and assertions are enabled, AssertionError will be thrown at runtime.


Some examples that use simple assertion form are as follows.

assert value > 5 ;
assert accontBalance > 0;
assert isStatusEnabled();


Read more at:

http:/www.roseindia.net/javacertification/scjp5/assertionsexample.shtml


Ads

Ads