strictfp Keyword in Java – Meaning and Why It Is Now Obsolete
strictfp ("strict floating-point") is a Java modifier that once forced floating-point calculations in a class, interface or method to follow the IEEE 754 standard exactly, so results were identical on every platform. Since Java 17 (JEP 306) all floating-point arithmetic is strict by default, so the keyword no longer has any effect, and the compiler warns that it is redundant.
This tutorial explains what strictfp was for, where it can be written, why it became obsolete, and what to use when you need reproducible or exact numeric results today.
1. What strictfp Meant
Java has always promised "write once, run anywhere", including for arithmetic. Early x86 processors, however, computed float and double values in 80-bit registers with extra precision and range. To run fast on such hardware, Java 1.2 introduced two modes:
| Mode | Behaviour (Java 1.2 – 16) |
|---|---|
| Default floating-point | Intermediate results could use extended exponent range, so results could differ slightly between platforms |
strictfp | Every intermediate result rounded to exact IEEE 754 single or double precision, giving bit-for-bit identical results everywhere |
The differences were tiny and appeared mostly with very large or very small numbers (overflow and underflow in intermediate steps), but they mattered for scientific code that needed reproducible results.
2. Where strictfp Can Be Used
- Classes (including nested classes): applies to all methods and initialisers in the class.
- Interfaces: applies to all default and static methods and nested types.
- Methods: applies to that method only (the method must have a body).
It cannot be used on variables, constructors or abstract methods.
public class StrictfpDemo {
// strictfp on a class applies to all its methods (legal, but redundant since Java 17)
static strictfp class Calculator {
double compound(double principal, double rate, int years) {
double amount = principal;
for (int i = 0; i < years; i++) amount *= (1 + rate / 100);
return amount;
}
}
// strictfp on a single method
static strictfp double average(double... values) {
double sum = 0;
for (double v : values) sum += v;
return sum / values.length;
}
public static void main(String[] args) {
System.out.println("compound: " + new Calculator().compound(10_000, 7.5, 3));
System.out.println("average: " + average(0.1, 0.2, 0.3));
System.out.println("Since Java 17 these results are identical with or without strictfp.");
}
}
compound: 12422.96875 average: 0.20000000000000004 Since Java 17 these results are identical with or without strictfp.
The code still compiles on modern Java, but javac -Xlint:strictfp (or simply -Xlint) prints: "as of release 17, all floating-point expressions are evaluated strictly and 'strictfp' is not required".
3. Why It Is Obsolete Since Java 17
Modern CPUs use SSE2 and later instruction sets, which perform exact IEEE 754 single and double arithmetic quickly. The performance reason for the non-strict mode disappeared, so JEP 306 ("Restore Always-Strict Floating-Point Semantics") made strict behaviour the only behaviour in Java 17:
- All floating-point expressions are evaluated strictly, with or without the keyword.
strictfpremains a reserved keyword and is still accepted, so old code compiles.- Adding or removing it changes nothing; you can safely delete it from code that targets Java 17+.
4. strictfp vs StrictMath
strictfp controlled basic arithmetic (+ - * /). Library functions such as sin, log and pow are a separate matter:
public class MathVsStrictMath {
public static void main(String[] args) {
double x = 1.2345;
// StrictMath always gives bit-for-bit identical results on every platform.
// Math may use faster CPU-specific implementations (results stay within documented accuracy).
System.out.println("StrictMath.sin(x) = " + StrictMath.sin(x));
System.out.println("Math.sin(x) = " + Math.sin(x));
System.out.println("0.1 + 0.2 = " + (0.1 + 0.2) + " (IEEE 754 rounding, on every JVM)");
}
}
StrictMath.sin(x) = 0.9439833239445111 Math.sin(x) = 0.9439833239445111 0.1 + 0.2 = 0.30000000000000004 (IEEE 754 rounding, on every JVM)
StrictMathuses fixed algorithms (fdlibm) and returns bit-for-bit identical results on every platform.Mathmay use faster, platform-specific intrinsics. Results stay within a documented error bound but may differ in the last bit between JVMs.- Use
StrictMathwhen exact reproducibility of transcendental functions matters, for example in simulations compared across machines.
5. Strict Does Not Mean Exact
Strict IEEE 754 arithmetic is reproducible, not precise in decimal terms. 0.1 + 0.2 is 0.30000000000000004 on every JVM, with or without strictfp. For money and other exact decimal values, use BigDecimal, or store amounts in the smallest unit (such as cents) as long.
6. Summary
strictfpforced exact IEEE 754 floating-point semantics in a class, interface or method.- It existed so results would be identical across platforms when default arithmetic was allowed extra range.
- Since Java 17 all floating-point arithmetic is strict, so the keyword has no effect and triggers a lint warning.
StrictMathgives reproducible library functions;BigDecimalgives exact decimal arithmetic.
Related Tutorials
Frequently Asked Questions (FAQ)
Q1. What is the strictfp keyword in Java?
A modifier that forced floating-point calculations in a class, interface or method to follow IEEE 754 exactly, so results were the same on every platform.
Q2. Is strictfp still needed in Java 17 and later?
No. Since Java 17 all floating-point arithmetic is strict by default (JEP 306), so the keyword has no effect.
Q3. Is strictfp deprecated?
It is not formally removed and still compiles, but it is redundant. javac -Xlint:strictfp warns that it is not required.
Q4. Where can strictfp be applied?
On classes, interfaces and concrete methods. It cannot be used on variables, constructors or abstract methods.
Q5. Can strictfp be used with abstract methods?
No. strictfp describes how a method body is evaluated, and an abstract method has no body. It can still be used on an abstract class.
Q6. Can a constructor be strictfp?
No. To make constructor code strict, the keyword had to be placed on the class.
Q7. Why was strictfp introduced?
From Java 1.2, default arithmetic could use extended exponent range on x87 hardware for speed. strictfp let programmers opt back into exact, portable results.
Q8. What is JEP 306?
"Restore Always-Strict Floating-Point Semantics", delivered in Java 17. It made strict evaluation the only mode, because modern CPUs perform it without a performance penalty.
Q9. Does strictfp make floating-point results exact?
No. It makes them reproducible. Binary floating-point still cannot represent values like 0.1 exactly; use BigDecimal for exact decimals.
Q10. What is the difference between strictfp and StrictMath?
strictfp governed basic arithmetic operators. StrictMath is a class whose functions (sin, log, pow and so on) return identical results on every platform.
Q11. What is the difference between Math and StrictMath?
Math may use faster platform-specific implementations whose results can differ in the last bit; StrictMath always uses the same algorithms and gives identical results.
Q12. Is strictfp inherited by subclasses?
No. It applied to the code written in the class itself; subclass methods were strict only if the subclass also declared strictfp.
Q13. Does strictfp affect integer arithmetic?
No. Integer arithmetic in Java has always been exact and identical on every platform; strictfp only concerned float and double.
Q14. Can an interface be declared strictfp?
Yes. It then applied to the interface's default and static methods and nested types.
Q15. Should I remove strictfp from old code?
If the code targets Java 17 or later, removing it is safe and changes nothing. If it must still run on older JVMs with identical results, keep it.
Q16. Is strictfp still a reserved keyword?
Yes. It cannot be used as an identifier, even though it no longer changes behaviour.
Q17. Did strictfp make programs slower?
On old x87 processors it could, because extra rounding steps were needed. On modern SSE2-based CPUs strict arithmetic is just as fast, which is why it became the default.
Q18. Can strictfp and abstract be used together on a class?
Yes. An abstract class can be strictfp; the modifier applies to its concrete methods.
Q19. What is IEEE 754?
The international standard for binary floating-point arithmetic. It defines formats such as 32-bit float and 64-bit double, rounding rules and special values like NaN and infinity.
Q20. Is strictfp asked in interviews?
Sometimes, as a question about rarely used keywords. The key answer: it guaranteed portable IEEE 754 results and has been redundant since Java 17.