assertions : Java Glossary

assertions

You do three types of sanity checking in your computer programs:

  1. to check for expected error conditions.
  2. to verify your code does not contain bugs.
  3. to make sure when your program goes insane, you find out about it as soon as possible before it destroys all traces of what went wrong.
For (1) you typically code explicit checks.

For (2) you use asserts which can be turned off once you are sure the code is solid.

For (3) you trust the run time array subscript out of bounds and null pointer checking.

If an assert fails, presumably something is wrong with a program or one of its resources — not data input files and keyed input. That is why it should be safe to turn it off for production. Sometimes I catch myself using assert to detect flawed data. There is no way the compiler knows that I am using the assert improperly.

Design by Contract Alternative
Enabling Learning More
Coding Links

Design by Contract

It is important to separate the logic for catching user errors from catching programmer errors. The logic for catching programmer errors can be safely turned off for speed once the program has been well tested.

Bertrand Meyer in Eiffel pioneered the use of structured assertions in a system he called Programming By Contract. These serve both to document the preconditions (require) on parameters coming into a method, and the guaranteed characteristics of the result (ensure). Unfortunately, Java still does not support them. They can be crudely added with the JaWa preprocessor.

Java version 1.4 or later has a feature Sun calls assertions but they are so pathetic they don’t deserve the name. It is as though Sun wanted to claim that Java has assertions without doing any work to implement them and not caring if the final product was actually of any use. If Sun wants to try again, they should look at how Eiffel assertions work.

They are new language feature, not just a new class.

Enabling

The main feature is the ability to turn them on and off at run time. This required a slight modification to the class file format. You use the -ea switch on the java.exe command line to enable or -da to disable assertion checking. To compile, you need the -source 1.4 or later switch on the javac.exe command line. ClassLoaders can conrol the enabling with ClassLoader. setClassAssertionStatus.

In Eclipse, you enable assertions by using the Java version 1.5 level then click run ⇒ arguments ⇒ VM arguments then enter -ea. There is no checkbox to tick.

Coding

assert a > b : "a must be bigger than b";

The second expression can be anything you would feed to printLn or it can be left out.

To mark a place in your code you should never get, you can code an assertion that always fails like this:

assert false : "Fell out bottom of loop without finding elt.";

Alternative

If you want an assertion that is always turned on, and works in all levels of the JDK (Java Development Kit), you can use code like this:

Learning More



CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/assertions.html J:\mindprod\jgloss\assertions.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.213]
You are visitor number 17,483.