Using Assertions

The assertion system is a fairly small topic but you need to know both the syntax of the system and the appropriate use of assertions.

The idea behind assertions is that, at critical points in your program, you can insert a single statement that can check for a required condition and produce a message if the condition does not exist. It is of course possible to write your own code without the assertion system. You could, for example, use if/else blocks or even exceptions to do this type of checking. The assertion system, however, offers a more concise syntax. During the normal run of a program assertions are turned off, so the checking causes no performance overhead.

To take examples from the real world, you might want to ensure ...

Get Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.