What is strictfp in Java?

Floating point calculations in Java are platform dependent. That means a different floating-point result is achieved when it is run on different hardware platformsi.e, 16/32/64 bit processors.

To address this issue, the strictfp keyword was added to JDK 1.2, which adheres to IEEE 754 rules for floating-point calculations.

How to use strictfp

The strictfp keyword can be used as a non-access modifier with classes, interfaces, and non-abstract methods only.

Some points to note:

  1. All methods declared in the class/interface, as well as all nested types stated in the class, are implicitly strictfp when a class or interface is declared with the strictfp modifier.

  2. The strictfp keyword cannot be used on variables, constructors, or abstract methods.

  3. strictfp cannot be used with any interface method because interface methods are implicitly abstract.

class Main {
public strictfp double difference()
{
double firstNumber = 11e+3;
double secondNumber = 2e+5;
return firstNumber - secondNumber;
}
public static strictfp void main(String[] args)
{
Main main = new Main();
System.out.println(main.difference());
}
}

Free Resources