|
|
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
As I will explain in Part 2 of this series, assertions help implement correct programs. Assertions were actually part of Oak, an early version of Java, but were jettisoned in a final push to get Java out of the lab and into the hands of Internet developers. As part of the Java Community Process, Java Specification Request 41 proposed adding a simple assertion facility to Java, prompting its welcome reappearance in J2SE (Java 2 Platform, Standard Edition) version 1.4.
An assertion is a boolean expression that a developer specifically proclaims to be true during program runtime execution. The simple idea of using assertions can have an unexpected influence on a software program's design and implementation. In this article I cover the mechanics of using the new assertion facility introduced in J2SE 1.4. In Part 2 I will cover the methodology of using assertions.
Read the whole series on J2SE 1.4's assertion capabilities:
You declare assertions with a new Java language keyword, assert. An assert statement has two permissible forms:
assert expression1;assert expression1 : expression2;In each form, expression1 is the boolean-typed expression being asserted. The expression represents a program condition that the developer specifically
proclaims must be true during program execution. In the second form, expression2 provides a means of passing a String message to the assertion facility. The following are a few examples of the first form:
assert 0 < value;assert ref != null;assert count == (oldCount + 1);assert ref.m1(parm);The asserted expression must be of type boolean, which the first three expressions obviously are. In the fourth expression, the method call m1(parm) must return a boolean result. A compile-time error occurs if expression1 does not evaluate to type boolean.
As an example of using assertions, class Foo listed below contains a simple assertion in the method m1(int):
public class Foo
{
public void m1( int value )
{
assert 0 <= value;
System.out.println( "OK" );
}
public static void main( String[] args )
{
Foo foo = new Foo();
System.out.print( "foo.m1( 1 ): " );
foo.m1( 1 );
System.out.print( "foo.m1( -1 ): " );
foo.m1( -1 );
}
}
The method main() calls m1(int) twice, once with a positive value and once with a negative value. The call with the negative value triggers an assertion
error. Since assert is a new Java keyword, to see this example in action, you must compile the class with a J2SE 1.4-compliant compiler. Furthermore,
the compiler requires a command-line option, -source 1.4, to signal source compilation using the assertion facility. Requiring a command-line switch to include assertions purportedly
protects backward compatibility.