Friday, July 5, 2024
HomeLanguagesJavastrictfp keyword in java

strictfp keyword in java

strictfp is a modifier that stands for strict floating-point which was not introduced in the base version of java as it was introduced in Java version 1.2. It is used in java for restricting floating-point calculations and ensuring the same result on every platform while performing operations in the floating-point variable. 
Floating-point calculations are platform-dependent i.e. different output(floating-point values) is achieved when a class file is run on different platforms(16/32/64 bit processors). To solve this type of issue, strictfp keyword was introduced in JDK 1.2 version by following IEEE 754 standards for floating-point calculations. 

Note: strictfp modifier is used with classes, interfaces, and methods only but is not applicable to apply with variables as illustrated below:

Illustration 1: Keyword usage with classes 

strictfp class Test {
   
    // All concrete methods here are implicitly strictfp.    
}

Illustration 2: Keyword usage with Interfaces 

strictfp interface Test {
   
    // All  methods here becomes implicitly 
    // strictfp when used during inheritance.    
}

class Car {
  
    // strictfp applied on a concrete method 
    strictfp void calculateSpeed(){}
}          

Illustration 3: Keyword usage with variables

strictfp interface Test {
    double sum();
    
    // Compile-time error here
    strictfp double mul(); 
}

Some conclusions can be drawn from the above illustrations as follows:

  • When a class or an interface is declared with strictfp modifier, then all methods declared in the class/interface, and all nested types declared in the class, are implicitly strictfp.
  • strictfp cannot be used with abstract methods. However, it can be used with abstract classes/interfaces.
  • Since methods of an interface are implicitly abstract, strictfp cannot be used with any method inside an interface.
  • From Java 17 version, strictfp keyword is NOT required explicitly as all floating point expressions are strictly evaluated

Example 

Java




// Java program to illustrate strictfp modifier
// Usage in Classes
 
// Main class
class GFG {
 
    // Method 1
    // Calculating sum using strictfp modifier
    public strictfp double sum()
    {
 
        double num1 = 10e+10;
        double num2 = 6e+08;
 
        // Returning the sum
        return (num1 + num2);
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating object of class in main() method
        GFG t = new GFG();
 
        // Here we have error of putting strictfp and
        // error is not found public static void main method
        System.out.println(t.sum());
    }
}


Output: 

This article is contributed by Gaurav Miglani. If you like Lazyroar and would like to contribute, you can also write an article using write.neveropen.co.za or mail your article to review-team@neveropen.co.za. See your article appearing on the Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments