DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

Part 4 - strictfp Keyword: IEEE 754, Floating-Point Precision

In the previous parts of this series, we learned about:

  • public and default class modifiers
  • final classes, methods, and variables
  • abstract classes and methods

In this final part, we will explore the strictfp keyword.

Unlike public, final, or abstract, the strictfp modifier is not commonly used in everyday Java development. However, it is an important Java language feature and a popular interview topic.

The main purpose of strictfp is to provide platform-independent floating-point calculations.

In this article, you will learn:

  • What strictfp means
  • Why floating-point calculations can differ across platforms
  • How IEEE 754 works
  • Where strictfp can be applied
  • Rules and restrictions
  • abstract strictfp combination
  • Interview questions
  • Complete Java Modifier Summary

What is strictfp in Java?

strictfp stands for:

Strict Floating Point

It is a modifier that ensures floating-point calculations follow the IEEE 754 standard.

The purpose of strictfp is to make floating-point results consistent across different platforms.

It can be applied to:

  • Classes
  • Methods

It cannot be applied to:

  • Variables

Syntax

strictfp Method

public strictfp void calculate() {

}
Enter fullscreen mode Exit fullscreen mode

strictfp Class

strictfp class Calculator {

}
Enter fullscreen mode Exit fullscreen mode

Why Do We Need strictfp?

Before Java introduced strictfp, floating-point calculations could produce slightly different results on different hardware platforms.

For example:

Windows Machine

10.0 / 3.0

Result:
3.3333333333333335


Linux Machine

10.0 / 3.0

Result:
3.333333333333333
Enter fullscreen mode Exit fullscreen mode

The difference happens because different processors may use different floating-point precision internally.

Java wanted:

Same Code

        ↓

Same Result

        ↓

Every Platform
Enter fullscreen mode Exit fullscreen mode

This is where strictfp helps.


What is IEEE 754?

IEEE 754 is an international standard for floating-point arithmetic.

It defines:

  • How floating-point numbers are stored
  • How calculations are performed
  • How rounding works

Java follows IEEE 754 rules for floating-point operations.


Floating Point Representation

A floating-point number consists of:

Floating Point Number

        Sign
          |
          ▼
     Exponent
          |
          ▼
     Fraction
Enter fullscreen mode Exit fullscreen mode

Example:

3.14
Enter fullscreen mode Exit fullscreen mode

Internally, the computer stores it in binary form.


How strictfp Works

Without strictfp:

Java Code

    ↓

CPU decides precision

    ↓

Possible different results
Enter fullscreen mode Exit fullscreen mode

With strictfp:

Java Code

    ↓

IEEE 754 Rules

    ↓

Same precision

    ↓

Same result everywhere
Enter fullscreen mode Exit fullscreen mode

Rule 1: strictfp Can Be Applied to Classes

Example:

strictfp class Calculator {

    public double calculate() {

        return 10.0 / 3.0;

    }

}
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Explanation

Step 1

The class is marked as strictfp.

Step 2

All concrete methods inside the class follow IEEE 754 rules.

Step 3

Floating-point operations use strict precision.

Step 4

The result becomes platform independent.


Rule 2: strictfp Can Be Applied to Methods

Example:

class Calculator {

    public strictfp double divide(double firstNumber, double secondNumber) {

        return firstNumber / secondNumber;

    }

}
Enter fullscreen mode Exit fullscreen mode

Only that method follows strict floating-point rules.


Rule 3: strictfp Cannot Be Applied to Variables

Incorrect:

class Calculator {

    strictfp double result = 10.5;

}
Enter fullscreen mode Exit fullscreen mode

Compile-Time Error

modifier strictfp not allowed here
Enter fullscreen mode Exit fullscreen mode

Why?

strictfp controls floating-point operations, not storage.

A variable only stores a value.


Rule 4: If a Class Is strictfp, All Concrete Methods Follow IEEE 754

Example:

strictfp class PaymentCalculator {

    public double calculateTax(double amount) {

        return amount * 0.18;

    }

}
Enter fullscreen mode Exit fullscreen mode

The calculateTax() method automatically follows strict floating-point rules.


Important Note: Java 17+ Change

Starting from Java 17, floating-point operations are always strict.

Earlier Java versions allowed some flexibility in floating-point calculations.

Therefore:

Java Version Behavior
Before Java 17 strictfp was needed for guaranteed strict behavior
Java 17 onwards Floating-point expressions are always strict

However, strictfp is still valid Java syntax and remains important for:

  • Reading old code
  • Understanding legacy systems
  • Java interviews

Illegal Combination: abstract strictfp

This is one of the most asked interview questions.

Let's understand why.

Abstract Method

An abstract method only provides a declaration.

Example:

abstract void calculate();
Enter fullscreen mode Exit fullscreen mode

It says:

"Someone else will provide the implementation."


strictfp Method

A strictfp method talks about how implementation should happen.

Example:

strictfp void calculate() {

}
Enter fullscreen mode Exit fullscreen mode

It says:

"Use IEEE 754 rules while executing this implementation."


These two concepts conflict.

One says:

No implementation exists.

The other says:

Here is how implementation should execute.

Therefore:

abstract strictfp void calculate();
Enter fullscreen mode Exit fullscreen mode

is illegal.


Compile-Time Error

modifier strictfp is not allowed here
Enter fullscreen mode Exit fullscreen mode

or

illegal combination of modifiers
Enter fullscreen mode Exit fullscreen mode

But abstract strictfp Class Is Legal

This is a tricky interview point.

Example:

abstract strictfp class Calculator {

    public abstract void calculate();

}
Enter fullscreen mode Exit fullscreen mode

This is valid.

Why?

Because:

  • The class can define strict rules for concrete child methods.
  • The class itself can still contain abstract methods.

Difference Between Abstract and strictfp

Feature Abstract strictfp
Purpose Defines incomplete behavior Defines floating-point precision
Applicable to Classes, Methods Classes, Methods
Talks about implementation? ❌ No ✅ Yes
Can apply to variables? ❌ No ❌ No
Can combine with each other? Only for classes Only for classes

Complete Java Modifier Summary

Now let's combine everything from this series.

Modifier Class Method Variable
public
default
private ❌ Top-level / ✅ Inner
protected ❌ Top-level / ✅ Inner
final
abstract
strictfp

Class Modifier Combination Rules

Combination Valid? Reason
abstract final class Cannot inherit and must inherit
abstract final method Cannot override and must override
abstract static method Static belongs to class
abstract private method Private cannot be overridden
abstract strictfp method Conflicting implementation rules
abstract strictfp class Both can coexist

Common Beginner Mistakes

Mistake 1: Thinking strictfp Makes Calculations Faster

Incorrect assumption:

strictfp improves performance.

Reality:

strictfp improves consistency, not speed.


Mistake 2: Using strictfp With Variables

Incorrect:

strictfp double amount;
Enter fullscreen mode Exit fullscreen mode

Correct:

strictfp class Calculator {

}
Enter fullscreen mode Exit fullscreen mode

or

strictfp double calculate() {

}
Enter fullscreen mode Exit fullscreen mode

Mistake 3: Thinking strictfp Is Required in Modern Java

In Java 17+, floating-point calculations are already strict.


Best Practices

  • Do not use strictfp in new Java 17+ applications unless required.
  • Understand it when maintaining older Java applications.
  • Use meaningful class and method design instead of relying on unnecessary modifiers.
  • Prefer readability over adding modifiers without a reason.

Interview Questions

1. What is the purpose of strictfp?

Answer:

strictfp ensures floating-point calculations follow IEEE 754 standards.

Why interviewers ask:

To test knowledge of Java floating-point behavior.


2. Where can strictfp be applied?

Answer:

  • Classes
  • Methods

Not variables.


3. Can strictfp be used with abstract methods?

Answer:

No.

Because abstract methods have no implementation, while strictfp defines implementation behavior.


4. Can abstract and strictfp be used together for classes?

Answer:

Yes.

Example:

abstract strictfp class Calculator {

}
Enter fullscreen mode Exit fullscreen mode

5. Is strictfp required in Java 17?

Answer:

No.

Java 17 already uses strict floating-point semantics.


Quick Memory Trick 🧠

Remember:

STRICT

S → Same Result
T → Takes IEEE Rules
R → Runs Everywhere
I → Independent of Platform
C → Consistent Calculation
T → True Precision
Enter fullscreen mode Exit fullscreen mode

strictfp means:

Same floating-point result everywhere.


Key Takeaways

  • strictfp controls floating-point calculation precision.
  • It follows IEEE 754 standards.
  • It can be applied to classes and methods.
  • It cannot be applied to variables.
  • A strictfp class makes all concrete methods strict.
  • abstract strictfp is illegal for methods but legal for classes.
  • Since Java 17, floating-point operations are already strict.
  • strictfp is mainly important for understanding legacy Java code and interviews.

If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.

Happy Coding!

Top comments (0)