Java : strictfp Modifier

What is strictfp in Java?

java strictfp keyword is modifier. strictfp used to restricting floating-point and round of  precision calculations (float and double) and ensuring same result on every platform while performing operations with floating point variable.

Why strictfp in java?

In java floating point calculations are platform dependent i.e. different precision output(floating point values) is achieved when a class file is run on different platforms(16/32/64 bit processors).

To solve such issues strictfp keyword was introduced in java 1.2 and later version by following IEEE 754 standards for floating-point calculations.

Where can we use strictfp?

Below are cases where strictfp use:


  • strictfp modifier is used with classes, interfaces and methods only.
  • When a class or an interface is declared with strictfp modifier, then all methods declared in the class/interface, and all nested types declared in the class, are implicitly follow strictfp.

Not Allowed

  • strictfp cannot be used with abstract methods. However, it can be used with abstract classes/interfaces.
  • strictfp cannot be used with any method inside an interface because methods of an interface are implicitly abstract,

How to use strictfp in java?

strictfp with class

strictfp class TestClass
    // all concrete methods here are
    // implicitly strictfp.

strictfp with interface

strictfp interface Test
    // all  methods here becomes implicitly
    // strictfp when used during inheritance.

strictfp with methods

class TestClass
    // strictfp applied on a concrete method
    strictfp void calculateSalary(){}

Note: strictfp cannot be used with any method inside an interface.

strictfp interface TestClass
    double sum();
    strictfp double multiply(); // compile-time error here


In this example applying strictfp in method where performing calculation and calling method where showing results so that show same result as calculated after applying strictfp.

public class StrictfpExample {

	public strictfp double addition(double a , double b)
         return (a+b);
	public strictfp double substraction(double a , double b)
         return (a-b);
	public strictfp double multiplication(double a , double b)
         return (a*b);
	public strictfp double division(double a , double b)
         return (a/b);
	public static void main(String[] args) {

		double num1 = 12e+10; 

        double num2 = 8e+08;

        StrictfpExample test=new StrictfpExample();

        System.out.println("Addition :"+test.addition(num1,num2));
        System.out.println("Substraction :"+test.substraction(num1,num2));
        System.out.println("Multiplication :"+test.multiplication(num1,num2));
        System.out.println("Division :"+test.division(num1,num2));



Addition :1.208E11
Substraction :1.192E11
Multiplication :9.6E19
Division :150.0