Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/articles/java-tutorials/)
-   -   Assertion in Java (http://www.go4expert.com/articles/assertion-in-java-t1628/)

S k 15Oct2006 13:05

Assertion in Java
 

Introduction



Assertion facility is added in J2SE 1.4. In order to support this facility J2SE 1.4 added the keyword assert to the language, and AssertionError class. An assertion checks a boolean-typed expression that must be true during program runtime execution. The assertion facility can be enabled or disable at runtime.

Declaring Assertion



Assertion statements have two forms as given below

assert expression;

assert expression1 : expression2;

The first form is simple form of assertion, while second form takes another expression. In both of the form boolean expression represents condition that must be evaluate to true runtime.

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();

The expression that has to be asserted runtime must be boolean value. In third example isStatusEnabled() must return boolean value. If condition evaluates to true, execution continues normally, otherwise the AssertionError is thrown.

Following program uses simple form of assertion

Code:

//AssertionDemo.java

Class AssertionDemo{

        Public static void main(String args[]){

                System.out.println( withdrawMoney(1000,500) );

                System.out.println( withdrawMoney(1000,2000) );

        }

        public double withdrawMoney(double balance , double amount){

                assert balance >= amount;

        return balance - amount;

        }

}

In above given example, main method calls withdrawMoney method with balance and amount as arguments. The withdrawMoney method has a assert statement that checks whether the balance is grater than or equal to amount to be withdrawn. In first call the method will execute without any exception, but in second call it AssertionError is thrown if the assertion is enabled at runtime.

Enable/Disable Assertions



By default assertion are not enabled, but compiler complains if assert is used as an identifier or label. The following command will compile AssertionDemo with assertion enabled.

javac -source 1.4 AssertionDemo.java

The resulting AssertionDemo class file will contain assertion code.

By default assertion are disabled in Java runtime environment. The argument -eanbleassertion or -ea will enables assertion, while -disableassertion or -da will disable assertions at runtime.

The following command will run AssertionDemo with assertion enabled.

Java -ea AssertionDemo

or

Java -enableassertion AssertionDemo

Second form of Assertion



The second form of assertion takes another expression as an argument.
The syntax is,

assert expression1 : expression2;

where expression1 is the condition and must evaluate to true at runtime.
This statement is equivalent to

assert expression1 : throw new AssertionError(expression2);

Note: AssertionError is unchecked exception, because it is inherited from Error class.

Here, expression2 must evaluate to some value.

By default AssertionError doesn't provide useful message so this form can be helpful to display some informative message to the user.

macknonalds 9Jan2007 17:37

Re: Assertion in Java
 
this is nice.... but what's the very best purpose why we need to use this one, pradeep?

I wish i could learn this one, so that i will be unique with my other java programmers... :-) thanks!

pradeep 10Jan2007 10:11

Re: Assertion in Java
 
In computer programming, an assertion is a programming language construct that checks whether an expression is true. Assertions are written so that they should always evaluate to true. If an assertion is false, it indicates a possible bug in the program. This is called an "assertion failure."

macknonalds 11Jan2007 16:09

Re: Assertion in Java
 
oh... thanks for that pradeep!

now i know! thanks again...

Balaji 13Feb2007 11:07

Re: Assertion in Java
 
Quote:

Originally Posted by macknonalds
this is nice.... but what's the very best purpose why we need to use this one, pradeep?

I wish i could learn this one, so that i will be unique with my other java programmers... :-) thanks!

Thanks a lot - Balaji


All times are GMT +5.5. The time now is 17:59.