Software Information |
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
MORE RESOURCES: Unable to open RSS Feed $XMLfilename with error HTTP ERROR: 404, exiting |
RELATED ARTICLES
Microsoft Axapta, Navision or Great Plains: ERP Selection for Large Corporation If you would like to pick something from Microsoft, or its ERP subdivision - Microsoft Business Solutions, and in case of large public company, you should consider these three: Axapta, Navision or Great Plains. We are not talking about Solomon here, because it is recommended for project organization. Microsoft Great Plains Customization Tools Evolution - Overview for Consultant When Great Plains Software introduced the first graphical accounting application for Mac and Windows in the beginning of 1990th it had Great Plains Dexterity customization tool, IDE and programming language. Dexterity design was based on several longevity principles: Graphical platform independence and database platform independence and easy switch from one DB to the other among the most important ones. Basic Steps To Optimize Your Internet Security After seeing many people complain about their weak Internet security I decided to write down some things that will help you for your Internet security.First, here are some tips to make windows safer :For basic security and update patches install Service Pack 2 for Windows XP or Service Pack 4 for Windows 2000. Recovering Microsoft Great Plains Customization - Tips for IT Director Remember nice and prosperous Clinton era? When you implemented innovative those old days accounting application - Great Plains Dynamics. And did a lot of customizations to fit your business requirements precisely. Microsoft Great Plains: Data Conversion & Migration Scenarios - Overview for Consultant Microsoft Business Solutions Great Plains serves multiple industries in the US, plus it has strong presence in Latin America, U.K. Think Of This Think of this, first we had the HAM Radio, then the bbs and now the Internet. Linux and the various other x systems are the next evolutionary step toward enhancing Global 2-way communications. Brand Your Websites URL With a Favicon Have you ever noticed that when you look at your browser favorites menu or the address bar, some entries have their own little icon beside their URL?It's called a "Favicon" (a graphic file with a .ico extension) and it's placed in the root directory of the web site. The Tools and Features of Adobe Photoshop Adobe Photoshop is one of the world's leading graphics editing programs, developed by Adobe Systems. Available on Mac OS or PC, Photoshop is used as one of the leading image design programs for the World Wide Web. How To Make Good Use of Spreadsheets Most computer users use spreadsheets software such as Microsoft Excel in their daily home and office. However very few are aware of the potential of spreadsheets in helping them in financial accounting and statistical analysis. Chinese Input - Step by Step Instruction on How to Input Chinese Characters in English Windows XP Enabling Chinese input is quick and easy, there are only a few steps you need go through to install the Chinese Language support and input method before you can type Chinese in your word processor or other program.We have broken the steps to 3 different sections for your easier to read and follow. Microsoft CRM: Data Conversion - Import from Act! Best Software Act! is very popular CRM for small and mid-size organization. This system attracts business owner by its low price, plus system is very easy to use. Microsoft eCommerce Web-development: Great Plains eConnect .Net - Highlights for Programmer In our small article we'll consider Microsoft Business Solutions Great Plains Sales Order Processing module as eCommerce backend. Plus we'll cover what is possible and impossible in eConnect and why. Screenplay and Script Writing Software When it comes to screenplay software each screenwriter needs to determine what they need from the program they purchase and pick one that fits their specific needs. Screenwriting software doesn't write the screenplay for you. Free Microsoft Word Online Training Tutorial Resources Microsoft Word is one of the most popular office applications that provide many features such as word processing, web publishing and database creation. Tapping into these Word resources, however, is not always easy and straightforward, leaving users stumped and puzzled. Pros and Cons of Using FREE Software in Your Business Itâ??s easy to understand why you might be drawn to the allure of free software. After all, it is free and free is always a good price to pay. Microsoft Great Plains: Service Business Customization & Integration Example Microsoft Business Solutions Great Plains might be considered as ERP platform, ready for customization and integration. Microsoft Business Solutions partner typically does one of the two things: specializes in the industry (in this case it goes for the nation-wide clientele) or specializes in the local or regional market. What To Do When Windows Wont Boot When Windows fails to boot it is normally caused by you installing a program or device and it has caused a conflict with one or more other programs.This will no doubt give you plenty of heartaches if you're not certain which program caused Windows to not boot up. How to Evaluate Staffing Software If you are in the market for new staffing software, I suspect that one of the most daunting of tasks will be to sift through the many vendors that are now servicing the staffing industry.You will find many very qualified companies with good staffing software products. Things You Can Do With Windows XP! Did you ever give a thought to the number of things you can do with Windows XP? Take your own time and think.. The Secret of the Layer Styles Dialogue When you double-click a layer in the Layer Palette, you will access the Layer Style dialogue box. Here, you can set many eye-catching layer styles such as Bevel & Emboss, Drop Shadow, Inner Shadow, etc. |
home | site map | contact us |