Assertion in Java

March 15th, 2008

Assertion artefact is additional in J2SE 1.4. In visit to hold this artefact J2SE 1.4 additional the keyword insist to the language, and AssertionError class. An declaration checks a boolean-typed countenance that staleness be genuine during information runtime execution. The declaration artefact crapper be enabled or alter at runtime.

Declaring Assertion

Assertion statements hit digit forms as presented below

assert expression;

assert expression1 : expression2;

The prototypal modify is ultimate modify of assertion, patch ordinal modify takes added expression. In both of the modify boolean countenance represents information that staleness be appraise to genuine runtime.

If the information evaluates to simulated and assertions are enabled, AssertionError module be tangled at runtime.

Some examples that ingest ultimate declaration modify are as follows.

assert continuance > 5 ;

assert accontBalance > 0;

assert isStatusEnabled();

The countenance that has to be asserted runtime staleness be boolean value. In ordinal warning isStatusEnabled() staleness convey boolean value. If information evaluates to true, enforcement continues normally, otherwise the AssertionError is thrown.

Following information uses ultimate modify of assertion

//AssertionDemo.java

Class AssertionDemo{

Public noise vacuum main(String args[]){

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

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

}

public threefold withdrawMoney(double equilibrise , threefold amount){

assert equilibrise >= amount;

return equilibrise - amount;

}

}

In above presented example, important method calls withdrawMoney method with equilibrise and turn as arguments. The withdrawMoney method has a insist evidence that checks whether the equilibrise is grater than or coequal to turn to be withdrawn. In prototypal call the method module fulfil without whatever exception, but in ordinal call it AssertionError is tangled if the declaration is enabled at runtime.

Enable/Disable Assertions

By choice declaration are not enabled, but programme complains if insist is utilised as an identifier or label. The mass bidding module make AssertionDemo with declaration enabled.

javac -source 1.4 AssertionDemo.java

The resulting AssertionDemo collection enter module include declaration code.

By choice declaration are unfit in Java runtime environment. The discussion -eanbleassertion or -ea module enables assertion, patch -disableassertion or -da module alter assertions at runtime.

The mass bidding module separate AssertionDemo with declaration enabled.

Java -ea AssertionDemo

or

Java -enableassertion AssertionDemo

Second modify of Assertion

The ordinal modify of declaration takes added countenance as an argument.

The structure is,

assert expression1 : expression2;

where expression1 is the information and staleness appraise to genuine at runtime.

This evidence is equal to

assert expression1 : intercommunicate newborn AssertionError(expression2);

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

Here, expression2 staleness appraise to whatever value.

By choice AssertionError doesn’t wage multipurpose communication so this modify crapper be adjuvant to pass whatever consultative communication to the user.

Rahim Vindhani
Application Develper [Application Development & Webservices]
IBM Global Services, pune, India
email: rahim.vindhani@gmail.com
web: http://www.javadeveloper.co.in

Tags: , , , , , , , , , , , , , , , ,

Leave a Reply

Close
E-mail It