«Back to Home

Core Java

Topics

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
  1. public strictfp class Bike { //valid...  
  2.   
  3.     public void run() { //strictfp applied on this method...  
  4.     }  
  5.     public void stop() { //strictfp applied on this method...  
  6.     }  
  7. }  
In class level, “strictfp” all the methods inside the class gets modified with “strictfp” keyword automatically.
 
Method level strictfp
  1. public class Car {  
  2.   
  3.     public strictfp void run() { //strictfp applied on this method...  
  4.     }  
  5.     public void stop() { //No strictfp on this method...  
  6.     }  
  7. }  
Interface level strictfp
  1. public strictfp interface Vehicle { // code...  
  2. }  
Where strictfp is not permitted?
  1. abstract public class Bike {  
  2.   
  3.     private strictfp float speed = 40//Can’t apply on variable  
  4.   
  5.     public strictfp Bike() { //Can’t apply on constructor  
  6.     }  
  7.     strictfp abstract void run(); //Can’t apply on abstract methods  
  8. }  
  9. }  
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.