Java : strictfp Modifier


What is strictfp in Java?

java strictfp keyword is modifier. strictfp used to restricting floating-point and round of  precision calculations (float and double) and ensuring same result on every platform while performing operations with floating point variable.

Why strictfp in java?

In java floating point calculations are platform dependent i.e. different precision output(floating point values) is achieved when a class file is run on different platforms(16/32/64 bit processors).

To solve such issues strictfp keyword was introduced in java 1.2 and later version by following IEEE 754 standards for floating-point calculations.

Where can we use strictfp?

Below are cases where strictfp use:

Allowed:

  • strictfp modifier is used with classes, interfaces and methods only.
  • When a class or an interface is declared with strictfp modifier, then all methods declared in the class/interface, and all nested types declared in the class, are implicitly follow strictfp.

Not Allowed

  • strictfp cannot be used with abstract methods. However, it can be used with abstract classes/interfaces.
  • strictfp cannot be used with any method inside an interface because methods of an interface are implicitly abstract,

How to use strictfp in java?

strictfp with class

strictfp class TestClass
{
    // all concrete methods here are
    // implicitly strictfp.
}

strictfp with interface

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

strictfp with methods

class TestClass
{
    // strictfp applied on a concrete method
    strictfp void calculateSalary(){}
}

Note: strictfp cannot be used with any method inside an interface.

strictfp interface TestClass
{
    double sum();
    strictfp double multiply(); // compile-time error here
}

Example 

In this example applying strictfp in method where performing calculation and calling method where showing results so that show same result as calculated after applying strictfp.

public class StrictfpExample {

	public strictfp double addition(double a , double b)
    {
         return (a+b);
    }
	public strictfp double substraction(double a , double b)
    {
         return (a-b);
    }
	public strictfp double multiplication(double a , double b)
    {
         return (a*b);
    }
	public strictfp double division(double a , double b)
    {
         return (a/b);
    }
	public static void main(String[] args) {

		double num1 = 12e+10; 

        double num2 = 8e+08;

        StrictfpExample test=new StrictfpExample();

        System.out.println("Addition :"+test.addition(num1,num2));
        System.out.println("Substraction :"+test.substraction(num1,num2));
        System.out.println("Multiplication :"+test.multiplication(num1,num2));
        System.out.println("Division :"+test.division(num1,num2));
	}

}

Output


Addition :1.208E11
Substraction :1.192E11
Multiplication :9.6E19
Division :150.0

References

https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.1.3

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s