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
If you have Microsoft Great Plains and support it for... Read More
Microsoft Business Solutions is now in process of creating so... Read More
Microsoft Business Solutions products: Great Plains, MS CRM, Navision, Axapta,... Read More
SyncUp, a file synchronizer is designed to assist the home... Read More
Microsoft Great Plains and Microsoft Retail Management System (Microsoft RMS)... Read More
ERP (Enterprise Resource Planning) Overview covers What is ERP, Brief... Read More
The major reason I recommend getting your hands on real... Read More
So, you've bought a new Macintosh, and now you may... Read More
Logistics automation is often considered as barcoding extension to Sales... Read More
IntroductionSurprised, by the title? well, this is a tour of... Read More
There are plenty of articles out there about how to... Read More
Former Great Plains Software Dynamics/eEnterprise, and currently Microsoft Business Solutions... Read More
1. With mapping software you can create a report that... Read More
Microsoft Business Solutions Navision serves both European and American megamarkets.... Read More
Crystal Reports is the most flexible tool on the market... Read More
MSN messenger is a pretty cool invention. I mean I'm... Read More
All of us know that Microsoft bought former Great Plains... Read More
If you use Microsoft Outlook (or similar applications) for e-mailing,... Read More
There is many things more frustrating than surfing a website... Read More
Let's say that you have a software project that's under... Read More
You've finally created databases that you can actually use to... Read More
Microsoft Business Solutions Great Plains has I'd say end user... Read More
Microsoft SQL Server is the leader for inexpensive and middle... Read More
Around the same time Microsoft made its move with .Net... Read More
What is 'adware'?Adware is basically software or scripts that are... Read More
on demand house cleaning Lake Forest ..Three highly respected names in Human Resources have joined forces... Read More
Fortunately one of the most common reasons cited for the... Read More
Professional services firm cuts costs and improves productivity with integrated... Read More
In order to implement VLANs in a network environment, you'll... Read More
How to delete the user? This is the first problem... Read More
People often ask me: What image file formats will Photoshop... Read More
Learning how to program software seems like a daunting task... Read More
Microsoft Business Solutions Great Plains was designed back in the... Read More
Introduction to C++Why Learn C++?C++ may at first seem like... Read More
TCO (Total Cost Ownership) is the buzzword in... Read More
Just the thought of a duel-boot scares many people away,... Read More
There are certain pluses and minuses in both cases and... Read More
Microsoft Great Plains has substantial mid-market share in the USA... Read More
Microsoft Great Plains is main Microsoft Business Solutions product, targeted... Read More
When making a decision to buy any piece of software... Read More
Microsoft bought Navision, Denmark based software development company, along with... Read More
Let's first look at your ERP system selection (without Retail... Read More
The US House of Representatives has recently passed the "Spy... Read More
RSS (Really Simple Syndication) is a way for a site... Read More
After almost two decades of existence, Quark has become the... Read More
This is the tutorial where we really get into programming.... Read More
Microsoft Great Plains, designed back in 1990th as database transferable... Read More
SAP Inc., a global leader in client/server enterprise application software... Read More
Microsoft Business Solutions Great Plains has I'd say end user... Read More
Executive SummaryAn effective plan for entering, cleaning and updating the... Read More
Software |