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

Mokena prom limo .. Lockport Chicago limo O’Hare
In The News:

Tech expert Kurt “CyberGuy" Knutsson says a malware app called SpyLend is posing as a financial tool, hitting 100,000-plus downloads.
Tech expert Kurt “CyberGuy" Knutsson says Clone Robotics’ Protoclone mimics humans with 1,000 muscles – too human for comfort.
Tech expert Kurt “CyberGuy" Knutsson urges you to use Apple’s App Privacy Report to see what your apps are really up to.
Tech expert Kurt “CyberGuy" Knutsson talks about how to smoothly transfer your data from an old laptop to a new one using cloud storage or external drives.
Kurt the CyberGuy offers tips to safeguard your personal information as scammers become more sophisticated with scam texts and other methods.
WeRide's unmanned electric delivery van, the Robovan W5, has 194 cubic feet of cargo space and can carry payloads up to about 2,205 pounds
Kurt the CyberGuy recommends deleting 16 browser extensions immediately in response to a report saying more than 3.2 million web users were affected by a security breach.
Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
A robotics firm in China claims a robot has performed the world's first humanoid robot front flip, which is significantly more difficult than a backflip.
A digital ID may be even more secure than a physical ID. Storing your driver's license in your Apple Wallet is an ideal solution for commuters.
A cybersecurity firm has shed some light on just how common tax scams have become. Kurt the CyberGuy offers his advice for how to protect your refund.
Lifesize Plans offers immersive walk-throughs that allow customers the opportunity to tour a home's design before construction actually begins.
Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
A man invested $4 million in bitcoin and lost it all with a single click when he was targeted by a vishing scam, which uses phone calls to trick people.
Expandable's Touchdown is a designer trailer that promises a luxury off-grid living experience and unfolds into a home in about 15 minutes.
Major employee screening company data breach affects 3.3 million people. Tech expert Kurt “CyberGuy" Knutsson explains what happened and gives five ways to protect your data.
Kurt “CyberGuy" Knutsson reveals mind-blowing tech that gives you sunlight on demand.
New malware "FrigidStealer" targets Mac computers. Tech expert Kurt “CyberGuy" Knutsson offers four ways to stay safe from infostealer malware.
Kurt “CyberGuy" Knutsson talks about how lab-grown muscles power a biohybrid hand for scalable prosthetics and research.
Reports of task scams have significantly risen in the past four years, according to the Federal Trade Commission. Kurt the CyberGuy offers some red flags.
iPhones have a hidden folder that eats up storage without you knowing it. Kurt the CyberGuy offers some tricks to free up storage on your device.
A recent ransomeware attack targeted Zacks, an American investment firm, and a cybercriminal claimed to have stolen records for millions of customers.
Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
Researchers unveiled a new soft prosthetic hand that combines the natural coordination patterns of our fingers with the decoding of motoneuron activity in the spinal column.
Accidentally deleting a text message on your iPhone or iPad is not a problem if you still need the message. Apple has a built-in way to recover it.

eCommerce development for Microsoft Great Plains: tools and highlights for programmer

Microsoft Business Solutions Great Plains, former Great Plains Software Dynamics... Read More

5 Easy File Management Tips in Microsoft Word

How many steps does it take you to locate and... Read More

Microsoft Great Plains Implementation for Midsize & Large Corporation: Lockbox Processing

Microsoft Great Plains is now targeting large and midsize businesses... Read More

Microsoft Business Solutions Products Selection: ERP, CRM, Retail Management

Let's first look at your ERP system selection (without Retail... Read More

ERP System of the Future: Database, Business Logic and Interface

We will base our prognosis on our Microsoft Business Solutions... Read More

Microsoft Navision and Crystal Reports - An Overview

Microsoft Business Solutions ? Navision is an integrated solution for... Read More

Why Stick With Email Clients Like Outlook?

Trying to figure out a stream in banning one email... Read More

Corporate Accounting System: Microsoft Great Plains ? Overview

Corporate ERP/MRP selection might be tough one, especially considering very... Read More

Anti-Virus Software

Anti-virus software is used to find, remove or fix files... Read More

Software Piracy

SOFTWARE PIRACY We regularly hear reports... Read More

Understanding XML Server

XML Server can be a Web Server that stores the... Read More

HelpDesk & Asset Management Software - Choosing The Right One

If you are to buy a HelpDesk & Asset Management... Read More

S is for Spying, Surveillance -- and for Software as Well

Words we choose to describe things and phenomena often show... Read More

IT Strategy for Midsize Business: Microsoft vs. Java, Great Plains & CRM

Midsize business or non-profit organization should decide if one-vendor solution... Read More

Daffodil DB: Web Database

What is a Web Database?A web database is a database... Read More

Tripwire for Linux File Integrity

What is Tripwire?Tripwire is a form intrusion detection system (IDS)... Read More

XML Parser and Their Types

XML parser is a software module to read documents and... Read More

Microsoft CRM Implementation - US Market Lessons

Microsoft CRM is CRM answer from Microsoft Business Solutions. If... Read More

Microsoft CRM Implementation & Customization: MS CRM Fax Gateway

With this small article we are continuing Microsoft Business Solutions... Read More

CRM 101: Customer Relationship Management for Beginners

Customer Relationship Management, abbreviated "CRM," is the term for a... Read More

Online PowerPoint Presentation ? Convert PowerPoint to Flash

Although we don't know whether Microsoft ever envisioned such a... Read More

Razzle Dazzle Them

Once upon a time not so long ago, there was... Read More

Great Plains Dexterity ? Microsoft Great Plains Customization Overview

Microsoft Business Solutions Great Plains, former Great Plains Software Dynamics... Read More

Microsoft Great Plains Integration Manager ? Advanced Techniques

Great Plains Integration Manager scripting and translation - overview for... Read More

Business Plan Software Review: Ultimate Business Planner?

One of the main reasons business owners and entrepreneurs use... Read More

Mokena Chicago limo O'Hare ..