strictfp keyword Overview

strictfp is a keyword in the Java programming language that restricts floating-point calculations to ensure portability. The strictfp command was introduced into Java with the Java Virtual Machine (JVM) version 1.2 and is available for use on all currently updated Java VMs.
Why strictfp keyword is used for float variable :
When a programmer perform operation in the floating point variable then the precision may differ from one platform to another platform that is why a programmer uses the strictfp keyword because the strictfp keyword ensures that it will provide same result on every platform.
Uses of strictfp keyword: -
  • It is used with class.
  • It is used with interface.
  • It is used with method.

 Example of strictfp keyword

strictfp class StrictfpDemo { float a=90.32342f; strictfp public void showFloat() { System.out.println(a); } public static void main(String args[]) { StrictfpDemo stfp=new StrictfpDemo(); stfp.showFloat(); } }

Output

D:\JavaProgram\strictfp>javac StrictfpDemo.java

D:\JavaProgram\strictfp>java StrictfpDemo
90.32342

Download this example
Illegal Uses of strictfp keyword: -
  • It cannot be used with abstract keyword.
  • It cannot be used with constructor.
  • It cannot be used with varibale.
strictfp class StrictfpDemo1 { strictfp float a=90.32342f; strictfp StrictfpDemo1() { a=98.786767f; } strictfp public void showFloat() { System.out.println(a); } public static void main(String args[]) { StrictfpDemo1 stfp=new StrictfpDemo1(); stfp.showFloat(); } }
Output
D:\JavaProgram\strictfp>javac StrictfpDemo1.java
StrictfpDemo1.java:3: modifier strictfp not allowed here
strictfp float a=90.32342f;
^
StrictfpDemo1.java:5: modifier strictfp not allowed here
strictfp StrictfpDemo1()
^
2 errors
Download this example
 



 

Latest Tutorial


Most Visited Tutorial