Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

1. Introduction

By default, the floating-point computations in Java are platform-dependent. And so, the floating-point outcome's precision depends on the hardware in-use.

In this tutorial, we'll learn how to use strictfp in Java to ensure platform-independent floating-point computations.

2. strictfp Usage

We can use the strictfp keyword as a non-access modifier for classes, non-abstract methods or interfaces:

public strictfp class ScientificCalculator {
    ...
    
    public double sum(double value1, double value2) {
        return value1 + value2;
    }

    public double diff(double value1, double value2) { 
        return value1 - value2; 
    }
}

public strictfp void calculateMarksPercentage() {
    ...
}

public strictfp interface Circle {
    double computeArea(double radius);
}

When we declare an interface or a class with strictfp, all of its member methods and other nested types inherit its behavior.

However, please note that we're not allowed to use strictfp keyword on variables, constructors or abstract methods.

Additionally, for cases when we have a superclass marked with it, it won't make our subclass inherit that behavior.

3. When to Use?

Java strictfp keyword comes handy whenever we care a great deal about the deterministic behavior of all floating-point computations:

@Test
public void whenMethodOfstrictfpClassInvoked_thenIdenticalResultOnAllPlatforms() {
    ScientificCalculator calculator = new ScientificCalculator();
    double result = calculator.sum(23e10, 98e17);
    assertThat(result, is(9.800000230000001E18));

    result = calculator.diff(Double.MAX_VALUE, 1.56);
    assertThat(result, is(1.7976931348623157E308));
}

Since the ScientificCalculator class makes use of this keyword, the above test case will pass on all hardware platforms. Please note that if we don't use it, JVM is free to use any extra precision available on the target platform hardware.

A popular real-world use-case for it is a system performing highly-sensitive medicinal calculations.

4. Conclusion

In this quick tutorial, we talked about when and how to use the strictfp keyword in Java.

As usual, all the presented code samples are available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!