strictfp keyword in java

The "strictfp" keyword in Java is used to force the precision of floating point calculations (both float and double) in Java conform to IEEE standard, explicitly.

If we don’t use strictfp keyword, the floating point precision value will be taken from the target platform’s hardware, i.e. CPU’s floating point processing capability.

In other words, If we use strictfp, result of floating point computations will be always same on all the platforms.

Since precision may differ from platform to platform ,Java programming language have provided the strictfp keyword, using which we can get the same result on all the platforms.

The strictfp keyword can be applied on methods, classes and interfaces.

  1. strictfp class StrictFPClassExample {
  2.     double num1 = 20e+50;
  3.     double num2 = 25e+10;
  4.     double add() {
  5.         return num1 + num2;
  6.     }
  7. }
strictfp class StrictFPClassExample {
    double num1 = 20e+50;
    double num2 = 25e+10;
    double add() {
        return num1 + num2;
    }
}

In the above example, all the floating point or double number computations within that class conform to IEEE standard

  1. strictfp interface StrictFPInterfaceExample {
  2.     double add();
  3. }
strictfp interface StrictFPInterfaceExample {
    double add();
}

In the above example, strictfp is applied on interface.

  1. class StrictFPMethodExample {
  2.     strictfp double add(double num1, double num2) {
  3.         return num1 + num2;
  4.     }
  5. }
class StrictFPMethodExample {
    strictfp double add(double num1, double num2) {
        return num1 + num2;
    }
}

In the above example, strictfp is applied on specific method in the class.

Rules for strictfp keyword

The strictfp keyword can’t be applied on abstract methods, variables or constructors.

  1. class StrictFPAbstractExample {  
  2.         strictfp abstract void add(double num1,double num2);//Illegal modifier
  3. }  
class StrictFPAbstractExample {  
        strictfp abstract void add(double num1,double num2);//Illegal modifier 
}  

  1. class StrictFPVariableExample {  
  2. strictfp double amount=100.123;//modifier strictfp not allowed here  
  3. }  
class StrictFPVariableExample {  
strictfp double amount=100.123;//modifier strictfp not allowed here  
}  

  1. class StrictFPConstructorExample {  
  2. strictfp StrictFPConstructorExample(){}//modifier strictfp not allowed here  
  3. }
class StrictFPConstructorExample {  
strictfp StrictFPConstructorExample(){}//modifier strictfp not allowed here  
}


The strictfp keyword can not be applied on any methods in interface.

  1. strictfp interface StrictFPInterface {
  2.    strictfp double multiply();    // compile error
  3. }
strictfp interface StrictFPInterface {
   strictfp double multiply();    // compile error
}


Why strictfp is used ?

As different platforms have different floating point hardware which calculates with more precision and greater range of values than the java specification requires which may produce diffrent output on diffrent plateforms.so it confirms the same output irrespective of diffrent plateforms.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on