Java Strictfp Keyword
Strictfp is used to impose limits on floating-point calculation. It ensures that we will get the same result on every platform while performing an operation with the floating-point variable.
The floating-point calculation is platform-dependent due to which the output may differ from platform to platform when a class file runs on different processors. That is why Java provides a Scriptfp keyword to resolve this issue.
Points to remember:
- "Scriptfp" modifier is used only with interfaces, classes, and methods.
- All methods of class/interface, as well as all nested methods declared, are implicitly scriptfp if class/interface is declared with scriptfp modifier.
- It can be used with abstract classes or interface but not with abstract methods.
- We cannot use sciptfp with any method inside an interface because all the methods inside an interface are implicitly abstract.
Let's understand the scriptfp modifier with a simple example.
public class Demo { // creating method sum using strictfp modifier publicstrictfp double sum() { double n1 = 25e+10; double n2 = 5e+09; return (n1 + n2); } public static strictfp void main(String[] args) { Test t = new Test(); System.out.println(t.sum()); } }