Java strictfp keyword, also a modifier was introduce in Java version 1.2. Java platforms uses it for restricting floating-point calculations in java. It is also for ensuring same result on every platform while performing operations in the floating-point variable. As floating points precision may vary from one platform to another, This keyword ensures the consistency across the platforms. As we know that 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).Β
Java program using strictfp keyword
strictfp modifier in java is used with classes, interfaces, and methods only but is not applicable to apply with variables. ‘strictfp’ keywordΒ cannotΒ be use with abstract methods. However, the user can use it with abstract classes/ interfaces. We can illustrate it as:
strictfp keyword with class
strictfp class DH {
// All methods here are implicitly strictfp.
}
strictfp keyword with interfaceΒ
strictfp interface DH {
// strictfp when used during inheritance.
}
class Car {
// strictfp applied on a method
strictfp void calculateSpeed(){}
}
strictfp keyword example
strictfp interface DH {
double sum();
// Compile-time error
strictfp double mul();
}