Software Information

Assertion in Java


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

//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.

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


MORE RESOURCES:

guardian.co.uk

Apple unveils software updates, new laptop
San Francisco Chronicle,  USA - 11 hours ago
The presentation included updates to the iLife and iWork software suites, a new 17-inch MacBook Pro with an 8-hour battery and a new digital rights ...
Video: Steve Jobs Weight Loss Due to Hormone Imbalance AssociatedPress
Apple's Macworld Surprise Is the Software PC Magazine
Summary: Software, MacBook Pro updates star in final Apple keynote Macworld
The Associated Press - Reuters
all 2,749 news articles


SAP Wants Oracle to Reveal Software, Support Profit Margins
PC World - 1 hour ago
SAP wants Oracle to reveal profit-margin information for JD Edwards and PeopleSoft software and support, according to a joint discovery statement filed this ...


DailyTech

ITunes embraces 3-tier pricing, will remove anti-copy software
Los Angeles Times, CA - 13 hours ago
Apple also updated much of its software. The latest version of iPhoto scans for individual faces so computer users can label photos of friends or family, ...
Apple removes protection software from songs TourDates.Co.Uk
Apple changes iTunes prices; removes copy protection USA Today
Apple changes iTunes pricing CNNMoney.com
Bloomberg - Shiny Shiny
all 1,053 news articles


HEB Mexico adds price optimization software
Bizjournals.com, NC - 3 hours ago
HEB’s Mexico subsidiary will begin using a price optimization software program currently in use by its parent company. Supermercados Internacionales HEB ...


Macworld

IBM Lotus Delivers New "Social" Notes and Free Symphony Software ...
CNNMoney.com - 22 hours ago
At Macworld, IBM (NYSE: IBM) today announced the availability of Lotus Notes 8.5 collaboration software with social computing features for all Mac OS X ...
Macworld: IBM finalizing free Symphony office suite for Macs Computerworld
Lotus Notes 8.5 ships, free IBM Symphony suite coming MacNN
IBM unveils Lotus Notes 8.5 VNUNet.com
eWeek - Computer Business Review
all 45 news articles


Macworld goes for the soft sell with budget-minded software
USA Today - 17 hours ago
Tuesday, the big news concerned 69-cent and $1.29 music downloads, and an upgrade to a $79 software suite. "In this economy, to focus on launching expensive ...


Plymouth software firm Paisley sold to Thomson Reuters
Bizjournals.com, NC - 1 hour ago
Paisley, a maker of risk-management and fraud investigation software, has been sold to Thomson Reuters’ tax and accounting business for an undisclosed ...


TrustedReviews

Apple Unveils Software, MacBook At Macworld
CNNMoney.com - Jan 6, 2009
The first hour of the speech was dominated by updates to Apple's software line, which remains an important element for the company, but more as a driver to ...
Apple rolls out 17-inch MacBook Pro, new software, iTunes Store tweaks DVICE
Apple unveils raft of new Mac software Times Online
Apple updates iTunes, iLife, unveils 17-inch MacBook Bizjournals.com
CNN International - Sydney Morning Herald
all 269 news articles


New York Times

Data Analysts Captivated by R’s Power
New York Times, United States - 4 hours ago
R first appeared in 1996, when the statistics professors Robert Gentleman, left, and Ross Ihaka released the code as a free software package. ...


CNET News

Sun Buys Cloud-computing Vendor Q-layer
PC World - 6 hours ago
Sun Microsystems has bought Q-layer, a Belgian vendor of software for automating the management of computing clouds, it said Wednesday. ...
Sun Acquires Cloud Computing Automator Q-layer eWeek
Sun acquires cloud computing vendor ZDNet
Sun Comes Up on Belgian Acquisition TheStreet.com
Register - CNET News
all 56 news articles

Software - Google News

home | site map
© 2006