What is strictfp in Java?
Floating point calculations in Java are platform dependent. That means a different floating-point result is achieved when it is run on
To address this issue, the strictfp keyword was added to JDK 1.2, which adheres to IEEE 754 rules for floating-point calculations.
How to use strictfp
The strictfp keyword can be used as a non-access modifier with classes, interfaces, and non-abstract methods only.
Some points to note:
-
All methods declared in the class/interface, as well as all nested types stated in the class, are implicitly
strictfpwhen a class or interface is declared with thestrictfpmodifier. -
The
strictfpkeyword cannot be used on variables, constructors, or abstract methods. -
strictfpcannot be used with any interface method because interface methods are implicitly abstract.
class Main {public strictfp double difference(){double firstNumber = 11e+3;double secondNumber = 2e+5;return firstNumber - secondNumber;}public static strictfp void main(String[] args){Main main = new Main();System.out.println(main.difference());}}