assert and strictfp Keywords in Java

Assert Statement

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. 

Use of Assertion

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:

  • The unreachable looking code is actually unreachable. e.g to assert if the default switch case is unreachable.
  • The state of the object. The assertions are mainly used to check logically impossible situations. For example, they can be used to check the state a code expects before it starts running or the state after it finishes running. Unlike normal exception/error handling, assertions are generally disabled at run-time.

Version 1

assert expression;

Example:

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);
 }   
}

Input:

Enter a value:25

Output:

Enter a value:25
The assertion evaluation remained true.

Input 2:

Enter a value:65

Output:

Enter a value:65
Exception in thread "main" java.lang.AssertionError
          at AssertionExample.assertValue(AssertionExample.java:4)
          at AssertionExample.main(AssertionExample.java:8)

Version 2

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);
 }   
}

Output:

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.

The strictfp keyword 

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:

  • class(A class declared as strictfp makes all its method members strictfp) 
  • interface(An interface declared as strictfp makes all its method members strictfp)
  • method(it needs to be a normal method, not the abstract methods)

Example:

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;
    }
}

Output:

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.