iSnare.com - Free Content Articles Directory
Authors Contents [Advanced Search][Add OpenSearch][Job Search]
Distribute your articles to thousands of article sites for only $2 and below! Read more...

Index  Computers and Technology
 

Assertion In Java

 
[ Contact the Author] [ Send to a Friend] [ Article Publisher] [Make PDF] [ Print] [ Bookmark & Share]
 
Read our Terms of Service before reprinting this article. The submitter specified above has claimed the rights to this article.
Rahim Vindhani

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.

Enabling Assertion

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 for 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.

Important NoticeDISCLAIMER: All information, content, and data in this article are sole opinions and/or findings of the individual user or organization that registered and submitted this article at Isnare.com without any fee. The article is strictly for educational or entertainment purposes only and should not be used in any way, implemented or applied without consultation from a professional. We at Isnare.com do not, in anyway, contribute or include our own findings, facts and opinions in any articles presented in this site. Publishing this article does not constitute Isnare.com's support or sponsorship for this article. Isnare.com is an article publishing service. Please read our Terms of Service for more information.

Article Tags: assertion [See Dictionary], assertionerror [See Dictionary], form [See Dictionary]
Got a question about this article? Ask the community!
Article published on January 14, 2006 at Isnare.com
 
Rate this article:

Java Arrays
Submitted by: Rahim Vindhani

Array is a fixed number collection of elements having same data type In java array is an object...

Hashtable
Submitted by: Rahim Vindhani

A collection allows a group of objects to be treated as a single unit Map is one of the core interfaces of java collection framework that defines operations for maintaining mappings of keys to values...

String In Java
Submitted by: Rahim Vindhani

Handling character strings in Java is supported through two final classes: String and StringBuffer The String class implements immutable character strings, which are read-only once the string has been created and initialized, whereas the StringBuffer class implements dynamic character strings...

Martin Yale 1217A Autofolder Review
Submitted by: Jeff McRitchie

For years the standard in paper folding machines, the Martin Yale Intimus 1217A is well-known in the small print industry for being a solid and flexible machine...

It’s a Mod Chip World!
Submitted by: Michiel Van Kets

No Nintendo Wii game console seems complete without a mod chip installation and with today’s latest mod chip innovations it’s easier than ever to buy and install your own Wii modification chip...

Martin Yale 400 Paper Jogging Machine Review
Submitted by: Jeff McRitchie

Any business that produces and binds a lot of documents on a regular basis should have a paper jogging machine on hand...

Laminating Film For Beginners
Submitted by: Jeff McRitchie

Roll laminators are awesome machines, but sometimes it can be difficult to know what supplies you need to use with your new laminating system...

PC200 Spiral Coil Binding Machine Review
Submitted by: Jeff McRitchie

The PC200 is positioned as a low-cost spiral coil binding solution for low volume users Here we take a look at this machine and examine its strengths and weaknesses...

Martin Yale 700E Paper Cutter Review
Submitted by: Jeff McRitchie

A commercial-quality paper cutter, the Martin Yale 700E is meant to be used in smaller print shops or in-house production floors for medium to large businesses...

Rhino Tuff CI 3000 Coil Inserter Review
Submitted by: Jeff McRitchie

Rhino's CI 3000 features a unique design that purports to make it easier to do spiral coil book binding...

Lamitek PhotoPro 13 Laminator Review
Submitted by: Jeff McRitchie

There are many laminators available and sometimes it is hard to know which one you should buy It is always a good idea to get a versatile machine, such as one that can do both hot and cold lamination, while also providing a crystal-clear finish...

Lamitek Photosmart 13 Laminator Review
Submitted by: Jeff McRitchie

The emergence and increasing numbers if digital printers has sparked an interest in laminating machines that can work with high-quality photos and/or glossier printed pages...

PC200E Spiral Coil Binding Machine Review
Submitted by: Jeff McRitchie

As the least expensive spiral coil binding machine that offers disengageable dies and an electric coil inserter, the PC200E is well positioned in the marketplace...

Be Careful When Buying Cheap Adobe Software
Submitted by: Adrianna Noton

When individuals are looking to buy software they always love finding cheap Adobe software However are these really great prices too good to be true...

What is the Difference Between Standard and High Yield Toner Cartridges?
Submitted by: Adriana N

There have been improvements in the manufacturing of printer toner cartridges Toner found in a cartridge is dry powder blended with a polymer that sticks on to the paper as printing takes place...

Inverted Microscope: A Great Tool For Studying Living Cells
Submitted by: Edison Rammsey

When you hear the term inverted microscope, you probably think of observing samples from under a microscope...

Digital Microscope: Eight Reasons Why You Must Have it Now!
Submitted by: Edison Rammsey

Welcome the Digital Age through a digital microscope With its eight benefits to be enjoyed, all other microscope will look small in comparison, pun intended...

Should Small Businesses Adapt to the Point of Sale System?
Submitted by: Adrianna Noton

In earlier times, a cash register along with a pen and notebook were sufficient means for processing and keeping track of transactions...

Isnare.com Footer Divider

© 2004-2009. Isnare Free Articles - An Isnare Online Technologies Free Articles Project. All Rights Reserved.   Privacy Policy