Java Keywords (Part XX): The strictfp Keyword

Java keyword list

abstract continue for new switch
assert default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const* float native super while
Keyword marked with an asterisk (*) are keywords that, although valid, are not used by programmers.
This is going to be an easy one. As of Java 17, this keyword is obsolete.

Prior to Java 17, this keyword was used to establish a strict floating-point (strict fp) policy. This meant that, when in use, this keyword guaranteed that floating point calculations would yield the same result across all hardware. When not in use, the Operating System had some leeway in refining precision of floating-point calculations. The keyword would be applied at the class, interface, or method. Since this is no longer the case with x86 architecture, Java 17 inherently does all floating point calculations in accordance with IEEE-754. In summary, different computer architectures were designed to handle floating point calculations for precision (strict) or for efficiency or speed (non strict). Becuase these differences in philosophies, porting Java across multiple platforms meant that floating point calculations were not guaranteed to yield the same results unless strictfp was used. Since modern computer architectures don't need x87 chips for these calculations, non-strict calculations are no longer needed.


public strictfp class MyCalculator {
    // Your code here
}

public class SomeClass {
    public strictfp double calculateSomething(double x, double y) {
        // DO some calculation
    }
}
Before Java 1.2, all floating point calculations were strict. This means that The IEEE 754 standard applied for both floating-point calculations and storage of floating-point values in various formats, including single (32-bit, used in Java's float) or double (64-bit, used in Java's double) precision. After Java 1.2, this keyword was created to allow the Operating System to manage underflows or overflows in a more flexible manner. This was allowed simply because strict floating point calculations were expensive for x87 architecture.

Comments

Popular posts from this blog

Combining State and Singleton Patterns to Create a State-Machine

The Beauty of the Null Object Pattern

Exception Handling: File CRUD Operations Example