strictfp keyword in Java

strictfp is one of the keywords introduced in Java to ensure portability and to restore platform independency.

Floating point calculations are dependent upon the underlying platform as precision may differ from platform to platform . strictfp keyword ensures that you will get the same result on every platform for floating point operations. With strictfp specified, JVM must toe the line very precisely according to the IEEE-754 floating-point specification.

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

strictfp class A{}

strictfp interface B{}

class C{
strictfp void method(){}
}

Note:If an interface or class is declared with strictfp, then all methods and nested types within that interface or class are implicitly strictfp.

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

When to use strictfp

strictfp is used, in case, where a programmer might need every platform to have precisely the same floating-point behaviour, even on platforms that could handle greater precision.

For example: Scientific applications rely heavily on floating- point arithmetic and, therefore, are affected by the precision and implementation of floating- point operations. Although the computers we use are IEEE compliant, this only assures the same representation of floating-point numbers; it does not guarantee that floating-point operations will be performed in the same way on all computers. As a result the same program run on different computers may yield different results. In this case, we can make sure of strictfp.

Leave a Reply

Your email address will not be published. Required fields are marked *