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
![]() |
|
![]() |
|
![]() |
|
![]() |
Microsoft Client Relation Management system (Microsoft CRM) and Microsoft RMS... Read More
Your computer cost you from hundreds to thousands of dollars,... Read More
Some companies that are in need of fleet management may... Read More
What is Spyware?Spyware monitors your surfing habits and sends the... Read More
Anti-virus software is used to find, remove or fix files... Read More
User interfaces and accessibility are some of the most important... Read More
Microsoft Great Plains and Microsoft Retail Management System (Microsoft RMS)... Read More
Do you remember that frustrating feeling when you find an... Read More
In linux, one of great commands for finding out information... Read More
Just when you thought you were Web savvy, one more... Read More
It would be easy to think, like most people apparently... Read More
It's all about turn times in the eMedia industry! The... Read More
Looks like Microsoft Great Plains becomes more and more popular,... Read More
Microsoft Business Solutions Great Plains fits to majority of horizontal... Read More
Great Plains Purchase Order Processing (POP) module makes up one-third... Read More
This short paper will expand on two key reasons to... Read More
Microsoft CRM customization techniques are very diversified and based on... Read More
Every day millions of new web documents emerge on the... Read More
You would like to protect your documents, wouldn't you? Reasons... Read More
In today's business world it's all but impossible to escape... Read More
C/SIDE (Client/Server Integrated Development Environment) - The core of... Read More
Microsoft Word is one of the most popular office applications... Read More
It is a well known fact that Java as a... Read More
While Ukraine is becoming a new popular IT outsourcing destination,... Read More
In the Clinton era the status quo was simple: you... Read More
Program Flow is what you think it is. How the... Read More
Current Microsoft Business Solutions Great Plains has more that 10... Read More
According to a survey conducted by InfoTrends/CAP Ventures entitled "Content-Centric... Read More
Do you want to get quality software at a reasonable... Read More
In 2004 Oracle, Inc. made its new step toward J2EE... Read More
What is Interactive Mapping?Interactive mapping is a visual display medium... Read More
Scrapbooks are very popular these days. I think that almost... Read More
IBM Lotus Domino or Microsoft Exchange?The severe competition continues for... Read More
If you're online using a dialup Internet connection, you'll probably... Read More
Microsoft Great Plains might be considered as ERP platform to... Read More
Microsoft Business Solutions CRM is present several years on the... Read More
Most people don't use Photoshop to its fullest capabilities. Here... Read More
Microsoft CRM has built-in conversion tool, however you should probably... Read More
It won't matter how effective your WinRunner Team is if... Read More
How would you like to prevent spyware and adware from... Read More
Microsoft CRM is CRM application, maintained and supported by Microsoft... Read More
One of the main reasons business owners and entrepreneurs use... Read More
There are two major WYSIWYG(What You See Is What You... Read More
In our small article we'll consider Microsoft Business Solutions Great... Read More
You've finally created databases that you can actually use to... Read More
The Microsoft system preparation utility for Windows 2000 is a... Read More
Which Type of Shop Can Rely On A Home Built... Read More
Microsoft Great Plains is main Microsoft Business Solutions accounting package... Read More
Great Plains Purchase Order Processing (POP) module makes up one-third... Read More
Microsoft Business Solutions Great Plains, former Great Plains Software Dynamics... Read More
Microsoft Business Solutions CRM is web-based CRM application, deploying all... Read More
What is IRC?IRC is Internet Relay Chat. It is a... Read More
In a previous article, I wrote about OpenOffice... Read More
Microsoft Business Solutions Great Plains and MS CRM (client relation... Read More
If you've been using MySQL database to store your important... Read More
Domino server is a buffer between the operation system and... Read More
Logistics automation is often considered as barcoding extension to Sales... Read More
Microsoft Business Solutions Great Plains is very popular ERP/MRP applications... Read More
We all already got used to computer monitoring both at... Read More
What is Spyware?Spyware monitors your surfing habits and sends the... Read More
What is Groupware?Have you ever had to manage document collaboration... Read More
Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More
The Software 2005 conference is now a wrap. This conference,... Read More
One of the things we can be as certain of... Read More
The objective for Zandi Digital is to make available clever... Read More
In this short FAQ style article we would like to... Read More
Software |