Java strictfp keyword : Explanation with example

Java strictfp keyword: why it is used for :

strictfp keyword is used for getting the same result of floating point calculation on different platforms. We cannot use strictfp with a class constructor or with methods defined inside an interface. But we can define an interface as strictfp and then all the methods defined inside that interface will become strictfp automatically. Similarly, we can use strictfp for class and methods also.

Using strictfp in classes :

strictfp class MyClass(){
    
}

In this example, all method inside the class MyClass automatically become strictfp.

Using on interface :

strictfp interface MyInterface{
    
}

Similarly, all the methods in this interface will become strictfp. But if we try to use strictfp on any inner method of an interface, it will throw compile time error.

first 3

Using with methods :

strictfp int findResult(){
    
}

Same as above, everything inside this method will follow strictfp .So, we can use it easily on methods, class or interfaces.

So, if we don’t use strictfp keyword, your program may provide different outputs on different platforms for floating point operations.

Similar tutorials :