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:


Washington Post

Apple offers App Store review guidelines (and updates iPhone software)
Washington Post
Apple has relented on two of the most contentious aspects of its control of its App Store: its restriction on software converted from releases for other ...
Apple Answers Questions About App Rejections, Raises OthersWired (blog)
Apple Relaxes App-Developer Restrictions >AAPLWall Street Journal
Blackberry Apps Remain Simply PowerfulTheStreet.com
Computerworld -Maximum Edge -CNET (blog)
all 920 news articles »


Broadcast Engineering

Miranda To Buy TV Software Maker OmniBus For C$48.7M
Wall Street Journal
T) said Thursday it has agreed to acquire OmniBus Systems Ltd., a maker of television broadcast software, from private-equity firm Palamon Capital Partners ...
Miranda Tech buys UK software co OmniBus for $47 mlnReuters
Palamon sells AV company OmniBus to Miranda TechnologiesAltAssets
Miranda Technologies Acquires IT-Based Playout Pioneer OmniBus SystemsMarketWatch (press release)
Proactive Investors USA & Canada
all 41 news articles »


Managing Automation Magazine

A Leading Private Label Food Manufacturer Implements CDC Software's Enterprise ...
MarketWatch (press release)
As a long time user of CDC Software solutions that include Ross ERP, CDC Factory and Pivotal CRM, Berner implemented EMF primarily to improve worker ...
CDC Adds PLM to ERP OfferingManaging Automation Magazine

all 25 news articles »


Take-Two Interactive Software, Inc. to Present at Two Investor Conferences
MarketWatch (press release)
Headquartered in New York City, Take-Two Interactive Software, Inc. is a global developer, marketer and publisher of interactive entertainment software ...

and more »


SAP Named Highest-Ranked Software Company in 2010 Dow Jones Sustainability Indexes
MarketWatch (press release)
For the software sector, DJSI reviewed a total of 30 companies. SAP had sector-leading scores in 11 of the 20 key dimensions, including customer ...

and more »


Nine Mile Software Inc. Announces First Revenues
MarketWatch (press release)
This validates our business model and the strength of our software, and demonstrates there is strong demand in the RIA market for affordable trading and ...

and more »


PC World

Hackers say they can “jailbreak” iPhone 4.1 software
VentureBeat
Less than a day after Apple released its iOS 4.1 operating system, hackers say they have jailbroken the software for the iPhone 4. In doing so, hackers have ...
Apple's iOS 4.1 software update for iPhone, iPod Touch arrivesUSA Today
Apple releases IOS 4.1 to fix faultsInquirer
Apple's iOS 4.1 software update out nowV3.co.uk
Telegraph.co.uk -ZDNet UK -Touch Reviews
all 1,220 news articles »


EETimes.com

Bend It with VirtualLogix
Data Center Journal
Red Bend Software was founded in 1999 and is today a software company that counts big companies like Sony Ericsson, Motorola, and LG Electronics as ...
VirtualLogix VLX(TM) Virtualization Software Now Integrates with Cortex A15 ...MarketWatch (press release)
Red Bend Buys VirtualLogix for Mobile VirtualizationPC World
Red Bend Software acquires VirtualLogixTrading Markets (press release)
Xconomy -Bizjournals.com
all 47 news articles »


Scientific American (blog)

New Robot May be a Strong Fit for Software Developers
TMCnet
The availability of more than 1000 software libraries appears to ensure a promising future for the young robot.” The AFP quotes Willow Garage co-director ...
Sock-pairing robot a promising match for software gurusAFP

all 47 news articles »

Google News

Home | Site Map

Powered By: Free Work At Home Business Opportunity!

© 2006