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
|
|
|
Dvd Burning Tips
CD and DVD replication is a process that works by creating discs from scratch. Not to be confused with duplication (the act of burning information onto readymade, commercially-available blank CD-Rs and DVD-Rs), replication relies on the use of molds to produce actual discs that already contain digital data. It is the preferred and recommended mode of mass production for quantities in excess of 500 units.Replication begins with a painstaking process called glass mastering. In this step, a laser is used to copy data onto a light-reactive glass plate. The plate's photosensitive glaze reacts to the heat of the l...(related: Software)
Great Plains Customization ? Programming Auto-apply In Accounts Receivable
Microsoft Great Plains is one of three Microsoft Business Solutions mid-market ERP products: Great Plains, Solomon, Navision. Considering that Great Plains is now very good candidate for integration with POS application, such as Microsoft Retail Management System or RMS and Client Relation Systems, such as Microsoft CRM ? there is common need in Great Plains customizations and integrations, especially on the level of MS SQL Se...(related: Software)
Microsoft Great Plains Version 8.5: Upgrade, Customization, Vba, Crystal Reports - Highlights
Microsoft Great Plains is one of the Microsoft Business Solutions family ERP products: Great Plains, Navision, Axapta, Solomon, Small Business Manager. MBS also has Microsoft CRM - Client Relation Management software and Microsoft Retail Management System (Microsoft RMS)Microsoft Great Plains 8.5 is scheduled to be released in the second quarter of 2005. It is usual practice when you here announcements and then you are notified about software release delays. Expect first the version to be released for US market and then you can expec...(related: Software)
Reduce Pop-ups And Annoying Ads
There is many things more frustrating than surfing a website only to have your screen suddenly full of pop-up advertising or a cascade of new windows opening. There are so many intrusive kinds of advertising on websites that there must be a way to deal with them. Luckily for us there is a few programs out th...(related: Software)
The Opera Alternative
Security flaws have long plagued Internet Explorer (IE), the market-dominating web browser from Microsoft. IE won the early browser wars, not only because it was free and bundled with Windows, but because it had some features and capabilities that its ...(related: Software)
The Xp Firewall Isnt Enough
You might think you don't need a firewall because windows XP has onebuilt in,but read on for a plain-english explanation why this isn't enough.Before broadband and cable connections were common most people didn't need apersonal firewall because they weren't connected to the Internet for extendedperiods of time.Every computer that connects to the internet has an IP address. This is likea sort of phone number, and is issued either to an individual computer or toa "gateway" that connects a private network to the 'net.Domestic dial-up internet connections normally use a system called DynamicIP Addressing where people are issued with a sort of "new identity" everytime they connect, so it's difficu...(related: Software)
Quick Summary Of Basic And Common Linux Commands
There are many commands that are used in linux on a daily basis, ones that everyone should know just to get by. Like back in the days of DOS, you had to know how to work with the command line and how to navigate around. Learning new commands is always hard, especially when there are so many new ones that don't always seem to make sense in their names.cdChange directory - this command is essential for traversing folders and moving about your linux computer. Its quite straight forward, cd /where/you/want/togo or cd .. to go up a level. It's pretty basic, but be aware of absolute and relative paths. If you are currently in the folder /home/puppy/pictur...(related: Software)
Microsoft Great Plains: Interest Calculation Example ? Stored Procedure For Crystal Report
This is intermediate level SQL scripting article for DB Administrator, Programmer, IT SpecialistOur and Microsoft Business Solutions goal here is to educate database administrator, programmer, software developer to enable them support Microsoft Great Plains for their companies. In our opinion self support is the goal of Microsoft to facilitate implementation of its products: Great Plains, Navision, Solomon, Microsoft CRM. You can do it for your company, appealing to Microsoft Business Solutions Techknowledge database. This will allow you to avoid expensive consultant visits onsite. You only need the help from professional when you plan on complex customization, interface or...(related: Software)
Microsoft Great Plains ? Licensing & Product Versions
Current Microsoft Business Solutions Great Plains has more that 10 years of history. Former Great Plains Software had two lines of Great Plains: Dynamics & Dynam...(related: Software)
Microsoft Great Plains Beverage Production & Distribution ? Implementation & Customization Highlight
Microsoft Business Solutions Great Plains has many years of successful implementation in multiple horizontal markets. In today's small article we'll share our experience of implementing and customizing Microsoft Great Plains, formerly Great Plains eEnterprise/Dynamics in beverage production and distribution industries. These examples will cover two scenarios: US regional distribution and International fruit punch production and sales in US, Latin America and Europe. We'll try to be industry specific and at the same time technical ? using Microsoft Great Plains technical...(related: Software)
What You Should Know About Installing Screensavers
Do you remember that frustrating feeling when you find an interesting screensaver and can't install it on your computer? This article will help you to never have it again.Back to when Windows 95 started to find its home on many computers there was just half a dozen of screensavers preinstalled with the system. All you could do was selecting one of these. It was as simple as boring.Luckily those times are left behind. Now we have hundreds of screensavers developed throughout the years and available for download. Finding a nice screensaver matching your taste and style can ...(related: Software)
site-map - Copyright © 2008 | Contact Webmaster | All Rights Reserved. | Software