![]() |
VOOZH | about |
In Java, the strictfp is a modifier that stands for strict floating-point which was not introduced in the base version of Java as it was introduced in Java version 1.2. It is used in Java 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 this type of issue, the strictfp keyword was introduced in the JDK 1.2 version by following IEEE 754 standards for floating-point calculations.
Note: strictfp modifier is used with classes, interfaces, and methods only but is not applicable to apply with variables as illustrated below:
Illustration 1: Keyword usage with classes
strictfp class Test {
// All concrete methods here are implicitly strictfp.
}
Illustration 2: Keyword usage with Interfaces
strictfp interface Test {
// All methods here becomes implicitly
// strictfp when used during inheritance.
}
class Car {
// strictfp applied on a concrete method
strictfp void calculateSpeed(){}
}
Illustration 3: Keyword usage with Abstract method in an Interface
strictfp interface Test {
double sum();
// Compile-time error here
strictfp double mul();
}
Some conclusions can be drawn from the above illustrations as follows:
Example:
1.006E11
Output:
Below we can see the output in console.