Amazing as it may seem engineers and programmers can make mistakes, the assertion keyword is designed to help detect these. Assertions also aid program maintainers by making it clear what the original coder had in mind.
If assertions are enabled at run time each time the assertion statement is encountered by the JVM its associated conditional is tested. If the test returns false the program terminates and an error message is produced. If, on the other hand, the test returns true no special action is take. So, by using the assert statement we are stating a condition that we belive should always be true if our program behaves as we expect.
Assertions have the following advantages:assert boolean expression; assert boolean expression : non-void expression;The first of these is demonstrated in the next listing. In the second form the non-void expression is passed as the argument to a AssertionError exception. The second listing on this page shows this form of assert.
/**
*Example of Java assertions
*/
class AssertionsInJava
{
public static void main(String[] args)
{
//We want to print all but the last argument.
//This is correct.
for (int i=0;i<args.length-1;i++)
{
assert(i+1<args.length);
System.out.println("arg is: "+args[i]);
}
//We want to print all but the last argument.
//There is a bug in the for statement.
for (int i=0;i<=args.length-1;i++)
{
assert(i+1<args.length);
System.out.println("arg is: "+args[i]);
}
}
}$
$
$ javac AssertionsInJava.java
$ java -ea AssertionsInJava one two
arg is: one
arg is: one
Exception in thread "main" java.lang.AssertionError
at AssertionsInJava.main(AssertionsInJava.java:22)
$
To disable assertions use the -da option and this is in fact the default.
$ java -da AssertionsInJava one two arg is: one arg is: one arg is: two $ java AssertionsInJava one two arg is: one arg is: one arg is: two $ $
/**
*Example of Java error test in Java. We demonstrate explicit testing, exceptions and assertions.
*/
//There is a bug in this code. Try entering a negative odd argument.
class AssertionsInJava2
{
public static void main(String[] args)
{
int iFirstArgument=0;
//We use explicit testing for simple errors that can reasonably
//be anticipated by the programmer.
if (args.length != 1)
{
System.err.println("Error expecting a single integer argument.");
System.exit(1);
}
//We use Exception testing for more difficult errors or where the programmer
//can't easily detect the error.
try
{
iFirstArgument = Integer.parseInt(args[0]);
}
catch (NumberFormatException e)
{
System.err.println("Error the argument was not an integer.");
System.exit(2);
}
int iRemainder = iFirstArgument % 2;
//We use asserts to spot errors in logic and other programming bugs.
//These shouldn't occur in the release version.
assert ((iRemainder==0)||(iRemainder==1)):"Expecting the remainder to be 0 or 1";
if (iRemainder==1)
{
System.out.println("The remainder is 1 so the input must be odd.");
}
else
{
//Bug
System.out.println("The remainder not 1 so the input must be even.");
}
}
}
$ javac AssertionsInJava2.java
$
$ java -ea AssertionsInJava2 -7
Exception in thread "main" java.lang.AssertionError: Expecting the remainder to be 0 or 1
at AssertionsInJava2.main(AssertionsInJava2.java:36)
$
$ java AssertionsInJava2 -7
The remainder not 1 so the input must be even.
$