Java Strictfp Keyword with Examples

In order to ensure that any floating point operation results in an identical result for all platforms, strictfp is used. In the case of floating points, precision can vary from one point to another. Consistency is guaranteed on every platform by the strictfp keyword.

strictfp can be applied to a class, method, or interface, but cannot be applied to abstract methods, variables, or constructors.

Example:

Below are valid examples of using strictfp Keyword in Java

strictfp class Test {
          }

strictfp interface Test {
          }

class Techvidvan {
          strictfp void Test() {
          }
          }

Illustration 1: Keyword usage with classes

strictfp class Test{
 
     // All concrete methods here are implicitly strictfp.    
       }

Illustration 2: Keyword usage with Interfaces

strictfp interface Test {
  
        // All  methods here becomes implicitly 
       // strictfp when used during inheritance.    
       }

       class TechVidvan{
       // strictfp applied on a concrete method 
        strictfp void calculateSpeed(){}
        }

Illustration 3: Keyword usage with variables:

strictfp interface Test {
 double sum();

 // Compile-time error here
 strictfp double mul(); 
 }

The following conclusions can be drawn from the above illustrations:

All methods defined in the class interface and all nested types declared in that class are usually strictfp by default when a class or interface is declared with an explicit modifier. It is not possible to use abstract methods with strictfp. In addition, it may be used with abstraction classes and interfaces. The strictfp is not allowed to use any method inside an interface due to the implicit abstraction of interfaces. The strictfp keyword is not a specified reword in Java 17.

From the examples in the table, it can be concluded that:

When a class or interface is specified with theStrictfp modifier, all methods declared in classesinterface.class and each nested type defined in these classes will be strictlyfpn by default. It is not possible to use an abstract method with strictfp. However, it is possible to use this function with an abstract class interface. Due to the implicit abstract nature of interface methods, strictfp cannot be employed for any method in an interface.

Example:

class TechVidvan{
    public strictfp double sum()
    {
        double num1 = 10e+10;
        double num2 = 6e+08;
        return (num1 + num2);
    }
    public static void main(String[] args)
    {
        TechVidvan t = new TechVidvan();
        System.out.println(t.sum());
    }
}

Output:
1.006E11

Legal code for Java strictfp keyword:

For methods, classes and interfaces it is possible to use the strictfp keyword.

strictfp class A{}//strictfp applied on class  
   strictfp interface M{}//strictfp applied on interface  
   class A{  
   strictfp void m(){}

Illegal code for Java strictfp keyword:

Abstract methods, variables and constructors are not supported by the strictfp keyword.
      class B{  
      strictfp abstract void m();//Illegal combination of modifiers  
      }  
      class B{  
      strictfp int data=10;//modifier strictfp not allowed here  
      }  
      class B{  
      strictfp B(){}//modifier strictfp not allowed here  
      }

When to Use?

If we care a lot about deterministic behavior of all FPFP calculations, Java’s strict keyword comes in handy:

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)); 
}
  • The test case will be passed to all hardware platforms as this keyword is being used by the ScientificCalculator class.
  • Note: If we are not using this feature, JVM will be allowed to apply a greater degree of accuracy on the target platform hardware.
  • The system which performs a high level of sensitivity medical calculations is a popular mainstream use case for it.

Java strictfp usage:

  • We can use the strictfp keyword as a non-access modifier for classes, non-abstract methods or interfaces.
  • When we declare an interface or class with strictfp, all its member methods and other nested types inherit its behavior.
  • Note, however, that the strictfp keyword is not allowed on variables, constructors, or abstract methods.
  • Please note that in variables, constructors and abstract methods we are not allowed to use the strictfp keyword.

Advantages of Strictfp in Java:

In Java, the advantages of Strictfp The benefits related to this keyword are shown at the bottom. From a developer’s point of view,

There are many advantages to applying strictfp; some of them can be found below:

  • The precision of the floating point on the various machines. strictfp uses precision and speed for floating-point operations. For 32 and 64 bit, it is totally unbiased with a clear focus on improved results.
  • Finally, using this keyword has no downside, no increase in time compression or execution speed.
  • It is important to understand that without the strictfp keyword and its function, floating-point output can be manipulated according to the precision point of the target machine’s CPU by the JVM and JIT compiler.
  • Just another way of ensuring that Java programming fundamentals, “write once run everywhere,” aren’t duplicated and don’t turn out badly around the globe except a source machine.

Rules to Remember:

Like any other keyword in any programming language, strictfp in Java has its uses and rules and to achieve the intended results, rules specified must be followed. Before we start to use that keyword, let us have a look at some of the rules.

  • The Strictfp keyword cannot be used by constructors; it’s working fine for classes, interfaces and methods.
  • It will implicitly implement strictfp for all methods and nested types when it is declared with an interface or a class.
  • An abstract class or interface, but not abstract methods, may be used to implement strictfp.
  • The strictfp function cannot be applied with variables in addition to the constructors; refer to the sample code below.
  • The correct use of strictfpnh will be ensured, with the avoidance of a possible difference between floatpoint accuracy which gives different results, in accordance with those rules.

Conclusion

We understood what a Strictfp Keyword in Java is, and then we explored the uses and were not to use this keyword. In accordance with the IEE 754 float point specification, it has been implemented in JVM 1.2. We’ve demonstrated the strictfp keyword with syntax, and we’ve implemented it in the examples. It’s a modification that will give you the same results.