The assertion is expected to be used in a program where the programmer suspects such a condition may exist. If the assert statement is used, and the condition does not occur wrongly, then the program will continue without any issue. If the condition occurs wrongly, then an error message is reported. Such checking involves a boolean expression. Of course, assertion coding must not necessarily involve the assert reserved word. There are many good programs that do assertion coding without the reserved word. However, using the assert reserved word brings out a particular feature in programming, making the program more constructive.
Consider the following code segment:
if (in != null) {
System.out.println("Do this.");
//other statements
} else
System.out.println("Test failed.");
Currently, the value of “in” is null, but it can be any integer. The if-bock will be executed if it is not null, which should be considered good. If it is null (false), the else part of the if-compound statement will be executed, reporting the issue.
Now, one assert statement can replace most parts of the if-compound statement. This is one classical use of the assert statement. Another classical use is to check if the limit of the result of a calculation was not reached, as the following description presents:
The speed of a moving object is given by,
where d is distance, t is the time taken, and s is the result (speed). It may be desired that the speed of the object stays below the speed of sound and so does not cause shock waves. In this case, the assert statement can be used to assert that the speed is below the speed of sound, which is 343 m/s.
A program code for this problem, without the assertion statement, can be as follows:
s = d / t;
if (s < 343) {
System.out.println("Do this.");
//other statements
} else
System.out.println("Test failed.");
Assertion Syntaxes in Java
In Java, the assertion syntaxes are in two forms. They are:
and
assert Expression1 : Expression2 ;
For the first option (statement), Expression1 is a Boolean expression. Note: Equals to, in a Boolean expression is == , because, in computing, = means assignment while == means equality. For the second option (statement), Expression1 is still a Boolean expression; but Expression2 results in a value, which may or may not be Boolean (true or false). However, the value must not be void. The purpose of Expression2 is for messaging the error to the programmer or the user. In this second option, the colon separates the two expressions. The colon does not apply to the first option.
Either assertion statement ultimately means true or false because of the Boolean expression. If it is true, the program continues as expected. If it is false, then the error should be reported.
assert Expression1
Validation
The example code for validation above, is repeated here for quick (visual) reference:
if (in != null) {
System.out.println("Do this.");
//other statements
} else
System.out.println("Test failed.");
“assert Expression1” can be used for this code as follows:
assert in != null;
System.out.println("Do this.");
//other statements
If assertion is enabled for the program, and if assertion asserts to false, as for this particular coding, then the statements below the assert statement will not be executed; and the Java Virtual machine (OS) will issue an error message. If assertion is not enabled for the program, the statements below the assert statement will be executed, despite being false. See how to enable assertion for a program below.
For this assert coding, the programmer allows the JVM to deal with error messaging entirely because of the absence of Expression2.
Preventing Reach of Limit
The example code to prevent reach-of-limit above, is repeated here for quick (visual) reference:
s = d / t;
if (s < 343) {
System.out.println("Do this.");
//other statements
} else
System.out.println("Test failed.");
"assert Expression1" can be used for this code as follows:
s = d / t;
assert s < 343;
System.out.println("Do this.");
//other statements
If assertion is enabled for the program, then for this particular coding, the statements below the assert statement will be executed, and the Java Virtual machine (OS) would not issue an error message; since the result of 5 is less than 343. If the assertion is not enabled, the statements below the assert statement will still be executed because they will be ignored even if it is false. See how to enable assertion for a program below.
For this assert coding, the programmer still allows the JVM to deal with error messaging entirely; because of the absence of Expression2.
Expression2
Validation
The example code for validation above, is repeated here for quick (visual) reference:
if (in != null) {
System.out.println("Do this.");
//other statements
} else
System.out.println("Test failed.");
Expression2 is for error reporting. The else-part of this if-compound statement, is for error reporting. A corresponding use of the second option assert statement, for this coding, can be as follows:
assert in != null : in;
System.out.println("Do this.");
//other statements
If assertion is enabled for the program, then for this particular coding, the statements below the assert statement will not be executed, and the Java Virtual machine (OS) will issue an error message. This time, the error message will use the variable’s value, in. If the assertion is not enabled, then the statements below the assert statement will be executed, despite the fact that the assert statement results in false. See how to enable assertion for a program below.
For this assert coding, the programmer influences the JVM in its dealing with error messaging because of the presence of Expression2 (in) – see details below.
Preventing Reach of Limit
The example code to prevent reach-of-limit above, is repeated here for quick (visual) reference:
s = d / t;
if (s < 343) {
System.out.println("Do this.");
//other statements
} else
System.out.println("Test failed.");
Expression2 is for error reporting. The else-part of this if-compound statement, is for error reporting. A corresponding use of the second option assert statement, for this coding, can be as follows:
s = d / t;
assert s < 343 : s;
System.out.println("Do this.");
//other statements
If assertion is enabled for the program, then for this particular coding, the statements below the assert statement will be executed, and the Java Virtual machine (OS) will not issue an error message; since the result of 5 is less than 343. If the result (value) of s is equal to or greater than 343, then the error message will involve the value of s. If the assertion is not enabled, the statements below the assert statement will still be executed because they will be ignored even if they result in false. See how to enable assertion for a program below.
For this assert coding, the programmer influences the JVM in its dealing with error messaging because of Expression2 (s) – see details below.
Enabling Assertion
If a program has assertion coding, the assert statement will only be effective to respect the assertion statement if the program is enabled. Enabling the assertion statement is done after the Java program has been compiled to bytecode. This is done at the command line when executing the program. It is done by including the switch -enableassertions. With switches at the command line, the – does not mean “take away”; it is a hyphen (bullet).
The use of the switch operates only for the execution of the program. If the switch is not used, the next time the program is called, the execution will ignore the assert statement.
In this section, only the code for validation will be addressed. The validation code with the first option assert statement is:
assert in != null;
System.out.println("Do this.");
//other statements
Assume that this is in the main() method of the main class and file, called TheClass. The file will normally be compiled with the command:
The compilation should go through successfully. With the switch, the resulting bytecode program, TheClass.class, will then be run at the command line with,
The report (output) will be:
at TheClass.main(TheClass.java:6)
Note that there is no mention of the value, null, for the variable, “in” at the output. With this, the statements below the assert statement in the program are not executed.
The validation code with the second option assert statement, which has “: in” is:
assert in != null : in;
System.out.println("Do this.");
//other statements
Assume that this is in the main() method of the main class and file, called TheClass. The file will be compiled with the command:
The compilation should go through successfully. With the switch, the resulting bytecode program, TheClass.class, will be run at the command line with,
The report (output) will be:
at TheClass.main(TheClass.java:6)
Note that there is mention of the value, null, for the variable, “in” at the output, as “: null” in the first output line. With this, the statements below the assert statement in the program are still not executed because of the enabling of any assert statement in the program. If the switch, -enableassertions, is omitted at the command line, any (or all) assert statements in the program will be ignored. The code below it will still be executed, even if the assert statements (boolean expressions) are false.
Conclusion
Assertion in Java is a statement based on the reserved word, assert, having a boolean expression and ending with a semicolon. It happens that certain conditions in a program may prevent it from working properly. The assert statement checks if such conditions exist in a program.
Good examples for using the assert statement are in validation and to check if the result of a calculation does not cross a limit. There are two forms of the assert statement: one which does not assist in the error message, and the other which assists in the error message. An assertion in Java is enabled at the command line, for the compiled bytecode, with the switch, -enableassertions.