Strictfp Keyword In Java
Strictfp Keyword
In Java, “strictfp” keyword ensures that we will get the same result on every platform. If we do any operations in the floating-point variable, the precision may be different from platform to platform.
Due to this, Java language has provided the “strictfp” keyword. Thus, we get same result on every platform. Hence, now we have a better control over the floating-point arithmetic.
Where we can use strictfp keyword?
Strictpf keyword can be applied on a class, interface and method in Java such as,
strictfp class X{}//strictfp with class
strictfp interface Y{}//strictfp with interface
class X{
strictfp void y(){}//strictfp with method
}
Class level strictfp
- public strictfp class Bike { //valid...
- public void run() { //strictfp applied on this method...
- }
- public void stop() { //strictfp applied on this method...
- }
- }
In class level, “strictfp” all the methods inside the class gets modified with “strictfp” keyword automatically.
Method level strictfp
- public class Car {
- public strictfp void run() { //strictfp applied on this method...
- }
- public void stop() { //No strictfp on this method...
- }
- }
Interface level strictfp
- public strictfp interface Vehicle { // code...
- }
Where strictfp is not permitted?
- abstract public class Bike {
- private strictfp float speed = 40; //Can’t apply on variable
- public strictfp Bike() { //Can’t apply on constructor
- }
- strictfp abstract void run(); //Can’t apply on abstract methods
- }
- }
Strictfp keyword forces floating points and any floating-point operations to adhere to the IEEE 754 standard. Hence, floating point results are always same on any platform.
Summary
Thus, we learnt “strictfp” keyword ensures that we will get the same result on every platform and also learnt the advantage of “strictfp” keyword in Java.