Custom Software Development
Free Resources
Custom Software
Database Development
Web Design
Web Services
Contact Us

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:

Earthtimes (press release)

Elliott Terminates Tender Offer to Acquire Epicor Software Corporation
MarketWatch - 9 hours ago
... LP and Elliott International, LP (collectively, "Elliott" or "we"), a major shareholder of Epicor Software Corporation (the "Company" or "Epicor"), ...
Hedge Fund Elliott Associates Withdraws Offer for Epicor Software Orange County Business Journal
Epicor drops after hedge fund ends hostile bid Forbes
UPDATE 1-Hedge Fund ends offer for Epicor Reuters
RTT News - Barron's Blogs
all 42 news articles


New York Times

The best thing about the 2.2 iPhone software update
CNET News, CA - 2 hours ago
When it some to iPhone software updates, I'm all about the basics. Apple could enable the iPhone to cook my dinner every night, but if it added multimedia ...
First Look: Apple's iPhone 2.2 Software Hits The Street (And ... CRN
Lots to like about new iPhone 2.2 software update Ars Technica
Apple releases iPhone Software v2.2 Apple Insider
G4 TV - Digitaltrends.com
all 93 news articles


PAR Technology Corporation Releases Next Version of SpaSoft(R) Spa ...
MarketWatch - 9 hours ago
An industry-standard for more than 10 years, SpaSoft is a fully integrated, dynamic activities management/scheduling software solution, ...


Traction Software Introduces Live Blog Micro-Messaging and End-of ...
MarketWatch - 6 hours ago
Live Blogs become a standard feature -- not an extra cost option -- for Traction Software's secure, scalable enterprise class hypertext platform which now ...


Progress Software and QAD Propose A Formula for The Perfect Lean ...
MarketWatch - 8 hours ago
a provider of leading application infrastructure software to develop, deploy, integrate and manage business applications today announced that Progress ...


ZDNet

Microsoft to offer free security software to attract beginners
eTaiwan News, Taiwan - 11 hours ago
19 to stop selling personal computer security software and to use free personal anti-virus software instead. The new software called Morro can support seven ...
Spamhaus: Microsoft Now 5th Most Spam Friendly ISP Washington Post
Microsoft: New software not Symantec, McAfee rival Reuters
Microsoft To Stop Paid PC Security Service, Offers Free Anti-Virus ... AHN
NetworkWorld.com - Wall Street Journal
all 304 news articles


Hann’s On Software bouht by Mediware
Bizjournals.com, NC - 4 hours ago
Mediware Information Systems Inc. has bought the assets of Hann’s On Software, a pharmacy-management software provider based in Santa Rosa, for $3.5 million ...
Mediware Acquisition Adds 320 Pharmacy Facilities MarketWatch
Mediware Information buys assets of Hann's On Software - Quick Facts RTT News
Mediware Acquisition Adds 320 Pharmacy Facilities International Business Times
all 19 news articles


Check Point Software Announces Participation in Fourth Quarter ...
MarketWatch - 7 hours ago
Check Point Software Technologies Ltd. ( www.checkpoint.com) is the leader in securing the Internet. Check Point offers total security solutions featuring a ...


DR Systems to Feature Software-Only, Enterprise Web-PACS for ...
MarketWatch - 8 hours ago
With the new zero-download application, clinicians no longer have to download the software before accessing reports and exams on their own computers. ...


AVG Sees Uphill Battle for Microsoft in Its Launch of Free Anti ...
MarketWatch - 7 hours ago
AVG, which for eight years has offered free anti-virus software to users worldwide, noted the multiple challenges Microsoft faces in supporting a free ...
AVG predicts 'uphill battle' for Microsoft's free anti-virus TechRadar UK
Microsoft Announces Upcoming Events for the Financial Community MarketWatch
all 27 news articles

Software - Google News

Article Index | home | site map
Powered by Custom Software Development - © 1995 - 2008 Nexus Software Systems