Site icon TechVidvan

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)); 
}

Java strictfp usage:

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:

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.

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.

Exit mobile version