Home Help Java A Java Assertions
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java Assertions

Advertisement
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


Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 3, 2006

Related Tutorials

Ask Questions?    Discuss: Java Assertions  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com