What is strictfp keyword in Java
This keyword is used to restrict the floating-point calculation in java and securing the same output while performing operations in the floating-point variable on every platform.
he calculation of floating points is platform-dependent. I.e it totally dependent on the platform configuration on which class file run (16/32/64 bit processors). To find the solution of these type issues we use strictfp keyword. It was initially launched in JDK 1.2 version following IEEE 754 standards of floating-point computations.
Important Points
➡ strictfp is a modifier.
➡ we can use strictfp keyword with classes, interfaces, and methods only.
➡ All the methods declared in the class/interface, and all nested types declared in the class, are implicitly strictfp When a class or an interface is declared with strictfp modifier
➡ We can use strictfp with abstract classes/interfaces.
➡ strictfp cannot be used with abstract
➡ Since methods of an interface are implicitly abstract, strictfp cannot be used with any method inside an interface.
- public class Main {
- public static void main(String[] args) {
- // Making Object of StrictFp
- StricfpExample stricfpexample = new StricfpExample();
- double sum = stricfpexample.sum(20e+10, 5e+08);
- System.out.println("Sum: " + sum);
- sum = stricfpexample.sum(20e+10, 5e+08);
- System.out.println("Sum: " + sum);
- }
- }
- public strictfp class StricfpExample {
- // Normal method
- public double sum(double num1, double num2) {
- return (num1 + num2);
- }
- // strictfp metheod
- public strictfp double sum(double num1, double num2, double num3) {
- return (num1 + num2 + num3);
- }
- }
Output:
Sum: 2.005E11
Sum: 2.055E11
Note :
The strictfp keyword cannot be applied on abstract methods, variables or constructors.
More To Read 👇👇
HashMap in JAVA (Structure and Working)