As per the dictionary, assertion means to state a fact or belief confidently and forcefully. In Java, the assertion is achieved using the assert statement. The assert statement is used to find the correctness of any assumption made. The assert statement is used with expression and its evaluation is presumed to be true
.
There are two versions of the assert statement.
assert expression;
assert expression1 : expression2;
If the evaluation fails, JVM throws an error, AssertionError.
The application that we develop is usually tested but still can't be perfect. There might be some data case that has been skipped. Assertions help to track the same and find the root cause of failure. The assertion is used for conditional cases and validates any assumption right at the beginning of the method.
A programmer can verify if Its assumptions are wrong or not. There may be several such assumptions like:
assert expression;
import java.util.Scanner;
public class AssertionExample{
public static void assertValue(int inp){
assert(inp<60);
System.out.println("The assertion evaluation remained true.");
}
public static void main( String args[] ){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a value:");
int value = scanner.nextInt();
assertValue(value);
}
}
Enter a value:25
Enter a value:25
The assertion evaluation remained true.
Enter a value:65
Enter a value:65
Exception in thread "main" java.lang.AssertionError
at AssertionExample.assertValue(AssertionExample.java:4)
at AssertionExample.main(AssertionExample.java:8)
assert expression1 : expression2;
The expression2 is the message which is printed when the expressions evaluate to false.
import java.util.Scanner;
public class AssertionExample{
public static void assertValue(int inp){
assert(inp<60):"You are a senior citizen";
System.out.println("The assertion evaluation remained true.");
}
public static void main( String args[] ){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a value:");
int value = scanner.nextInt();
assertValue(value);
}
}
Enter a value:65
Exception in thread "main" java.lang.AssertionError:You are a senior citizen
at AssertionExample.assertValue(AssertionExample.java:4)
at AssertionExample.main(AssertionExample.java:8)
Notice the message printed.
While performing an operation in the floating-point variable, we see the output to vary regarding precision from one platform to another. To ensure uniformity in the output, so that we the same output and better control on floating arithmetic regardless of the platform, we use strictfp.
strictfp is used in association with the followings:
public class StrictFPClass {
public static void main(String []args){
double num1 = 10e+102;
double num2 = 6e+08;
double sum = calculate(num1,num2);
System.out.println("The sum is "+sum);
}
//This is a strictfp method
public static strictfp double calculate(double num1, double num2) {
return num1 + num2;
}
}
The sum is 1.0E103
The output will be the same regardless of the platform.
This was an overview to assert statements and stritfp keyword. We will discuss the Generics in the next lecture.