Monday, March 28, 2011

Java strictfp keyword

strictfp is a keyword in the Java programming language that restricts floating-point calculations to ensure portability. Using strictfp guarantees that results of floating-point calculations are identical on all platforms. It was introduced into version 1.2.

Just an example: x86 processors support 80 bit double values. This type is used for intermidiate opetations in Java, to avoid buffer overflows and underflows. The result value is stored according IEEE 754 standard, as 64 bit double. This is when we do not use strictfp keyword.

When we add strictfp, intermidiate java calculations are also reduced to 64 bits. The chance for underflows/overflows is greater but we keep the the results will be compatible with results in Java 1.1.

Usage
strictfp can be used on classes, interfaces and non-abstract methods. When applied to a method, it causes all calculations inside the method to use strict floating-point math. When applied to a class, all calculations inside the class use strict floating-point math.

No comments:

Post a Comment