What is use of strictfp keyword in java

You are here : Home / Core Java Tutorials /

In this tutorial we will learn what is use of strictfp keyword in java, when to use strictfp keyword in java.

Contents of page >
  • strictfp keyword was introduced in which java version?
  • What is use of strictfp keyword in java?
  • What will happen if you don’t use strictfp keyword in java?
  • To all what does strictfp keyword can be applied in java?
    • Let’s see examples where and how to use strictfp keyword in java.
  • To all what does strictfp keyword can NOT be applied in java?
    • Let’s see examples where not to use strictfp keyword in java.
  • What JLS (java language specification say about) strictfp keyword ?



strictfp keyword was introduced in which java version?
strictfp keyword was introduced in Java 2. i.e. in second version of java.

What is use of strictfp keyword in java?
strictfp keyword ensures that you always get same results from floating point calculations on different platforms (like windows, linux, Mac os).

What will happen if you don’t use strictfp keyword in java?
If we don't use strictfp, floating point calculations results may vary on different platforms. Because JVM might might take different precision on different platforms (As we all know that JVM is platform independent).

To all what does strictfp keyword can be applied in java?
strictfp keyword can be applied to following in java -

Let’s see examples where and how to use strictfp keyword in java.

Applying strictfp keyword to interface in java -
strictfp interface MyInterface{ //Applying strictfp keyword to interface in java
}

Applying strictfp keyword to class in java -
strictfp class MyClass{ //Applying strictfp keyword to class in java
}

Applying strictfp keyword to method in java -
strictfp class MyClass {
   strictfp void myMethod(){ //Applying strictfp keyword to method in java
         
   }
}



To all what does strictfp keyword can NOT be applied in java?

Let’s see examples where not to use strictfp keyword in java.

> You cannot apply strictfp keyword to abstract method in java -
You will get compilation error to remove invalid identifier.

Hence, you also cannot apply strictfp keyword to methods of interface in java (Because all methods of interface are abstract by default) -

> You cannot apply strictfp keyword to member variable of class or interface in java -

> You cannot apply strictfp keyword to constructor in java.

What JLS (java language specification say about) strictfp keyword ?
JLS says this about strictfp keyword in java -
“Within an FP-strict expression, all intermediate values must be elements of the float value set or the double value set, implying that the results of all FP-strict expressions must be those predicted by IEEE 754 arithmetic on operands represented using single and double formats. Within an expression that is not FP-strict, some leeway is granted for an implementation to use an extended exponent range to represent intermediate results; the net effect, roughly speaking, is that a calculation might produce "the correct answer" in situations where exclusive use of the float value set or double value set might result in overflow or underflow.”


Summary -
So, in this core java tutorial we learned what is What is use of strictfp keyword in java? strictfp keyword was introduced in which java version? What will happen if you don’t use strictfp keyword in java? To all what does strictfp keyword can be applied in java? To all what does strictfp keyword can NOT be applied in java? examples where to and not to use strictfp keyword in java. What JLS (java language specification say about) strictfp keyword?




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.




RELATED LINKS>

eEdit
Must read for you :