Java Strictfp Keyword

javalearning Published on 15 February 2020

strictfp is a keyword in java used for restricting floating-point calculations and ensuring the same result on every platform while performing operations in the floating-point variable.


Floating-point calculations are platform-dependent i.e. different output(floating-point values) is achieved when a class file is run on different platforms(16/32/64 bit processors). To solve these types of issues, strictfp keyword was introduced in JDK 1.2 version by following IEEE 754 standards for floating-point calculations.


Important Points


  1. strictfp modifier is used with classes, interfaces, and methods only.
  2. 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 strictfp.
  3. strictfp cannot be used with abstract methods. However, it can be used with abstract classes/interfaces.
  4. Since methods of an interface are implicitly abstract, strictfp cannot be used with any method inside an interface.


  1. public class Main {
  2. public static void main(String[] args) {
  3. // Making Object of StrictFp
  4. StricfpExample stricfpexample = new StricfpExample();
  5. double sum = stricfpexample.sum(20e+10, 5e+08);
  6. System.out.println("Sum: " + sum);
  7. sum = stricfpexample.sum(20e+10, 5e+08);
  8. System.out.println("Sum: " + sum);
  9. }
  10. }
  11. public strictfp class StricfpExample {
  12. // Normal method
  13. public double sum(double num1, double num2) {
  14. return (num1 + num2);
  15. }
  16. // strictfp metheod
  17. public strictfp double sum(double num1, double num2, double num3) {
  18. return (num1 + num2 + num3);
  19. }
  20. }

Output

Sum: 2.005E11

Sum: 2.055E11


Note :

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