In Java, the strictfp keyword is used to force the precision of floating point calculations (float or double) in Java conform to IEEE’s 754 standard, explicitly. Without using strictfp keyword, the floating point precision depends on target platform’s hardware, i.e. CPU’s floating point processing capability. In other words, using strictfp ensures result of floating point computations is always same on all platforms.

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

 

Some Rules about strictfp:

    •           strictfp cannot be applied for constructors.
    •          If an interface or class is declared with strictfp, then all methods and nested types within that interface or class are implicitly strictfp.
    •           strictfp cannot be applied for interface methods.

 

Java strictfp keyword Examples:

The following class is declared with strictfp, hence all the floating point computations within that class conform to IEEE’s 754 standard:

strictfp class StrictFPClass {
    double num1 = 10e+102;
    double num2 = 6e+08;
    double calculate() {
        return num1 + num2;
    }
} 

The following interface is declared with strictfp, but its methods cannot:

strictfp interface StrictFPInterface {
    double calculate();
    strictfp double compute();    // compile error
} 

The following method is declared with strictfp:

class StrictFPMethod {
    strictfp double computeTotal(double x, double y) {
        return x + y;
    }
}

See all keywords in Java.

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#5Art2016-11-30 11:18
@gp none. It seems that author just wanted to show that interface methods cannot be declared with strictfp - will cause compiler error.
Quote
#4gp2016-11-30 10:55
which class is implementing StrictFPInterface interface in this link? I am bit unclear on that.
Quote
#3pradip patil2015-11-24 22:29
how strictfp works in inheritance ,
If parent class is strictfp
then it need to declare class as strictfp??
Quote
#2mandeep2015-09-27 00:12
No, strictfp can't be applied to variables...
Quote
#1Art2015-09-19 11:05
strictfp can't be applied to variables, can it?
Quote