Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

J2SE 1.4 premieres Java's assertion capabilities, Part 1

Understand the mechanics of Java's new assertion facility

  • Print
  • Feedback

Page 5 of 5

public class Pathological
{
  private String ping( boolean test )
  {
    System.err.print( "ping ... " );
    assert test : pong( test );
    return null;
  }
  private String pong( boolean test )
  {
    System.err.print( "pong ... " );
    assert test : ping( test );
    return null;
  }
  public static void main( String[] args )
  {
    Pathological pathological = new Pathological();
    System.err.println( "Pathological.ping( false ): " );
    pathological.ping( false );
  }
}


Though highly stylized, this example hints at the dangers of writing complex and oblique assert statements.

Be assertive

Assertions are an important addition to the Java programming language. By using assertions, developers can clearly demark the boundaries of acceptable program behavior.

This article exhaustively details the mechanics of using J2SE 1.4's assertions. Fortunately, you only need to digest most of these sticky details once. Using the simple assertion facility is relatively straightforward, so you can quickly add assertions to your development routine. Regrettably, neither the J2SE 1.4 compiler nor its runtime system enable the assertion facility by default. Each requires new command-line switches.

Part 2 of this article will discuss the methodological issues regarding assertions and how the Java assertion facility compares to Design by Contract. It also challenges the convention of using the Java exception mechanism for handling argument checking in public methods. Until then, remember to be assertive with your assertions.

About the author

Wm. Paul Rogers is a Java/object-oriented architect whose interests include teaching an understanding of Java design and implementation through stressing the first principles of fundamental object-oriented programming. He began using Java in the fall of 1995 in support of oceanographic studies conducted at the Monterey Bay Aquarium Research Institute, where he led the charge in utilizing new technologies to expand the possibilities of ocean science research. Paul has been using object-oriented methods for 10 years and works as an independent consultant in Bellingham, Wash., where he also teaches computer science at Western Washington University.

Read more about Core Java in JavaWorld's Core Java section.

  • Print
  • Feedback

Resources
  • Browse our Topical Index for more stories on the Java 2 Platform, Standard Edition: http://www.javaworld.com/channel_content/jw-j2se-index.shtml
  • Read more JavaWorld articles by Wm. Paul Rogers