Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
strictfp
floatingpoint
communitycreator

What is strictfp in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Floating point calculations in Java are platform dependent. That means a different floating-point result is achieved when it is run on different hardware platformsi.e, 16/32/64 bit processors.

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:

  1. All methods declared in the class/interface, as well as all nested types stated in the class, are implicitly strictfp when a class or interface is declared with the strictfp modifier.

  2. The strictfp keyword cannot be used on variables, constructors, or abstract methods.

  3. strictfp cannot 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());
}
}

RELATED TAGS

java
strictfp
floatingpoint
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring