Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials

>Home>Java Tutorial>Use of strictfp modifier in Java

Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials



The Java strictfp Modifier, Use of strictfp Keyword

The floating point operations performed in the Java programs may give different results in terms of precisions if executed on multiple computers.

In Java use of strictfp modifier is to get exactly the same results from your floating point calculations on every platform. If you don't use strictfp, the JVM implementation is free to use extra precision where available.

If a method or class or an interface is declared as strictfp then all floating point calculations in it has to follow IEEE 754 standard, so that you will get platform independent results.


Rules of strictfp in Java

  • The strictfp modifier is applicable only for non abstract methods, classes and interfaces.

  • The strictfp method always focus on implementation where as abstract method never focus on implementation, therefore strictfp – abstract method combination is invalid.

  • The abstract – strictfp combination is valid for classes but illegal for methods.

  • If a class declared as strictfp then every concrete method in that class has to follow IEEE 754 standard, so that you will get platform independent result.

Possible use of strictfp in Java with example

Example 1: Allowed for interface but not for abstract methods


strictfp interface Calculator
{
public abstract strictfp double addition(double a,double b);
// error: modifier strictfp not allowed here
}

Example 2: Allowed for abstract class and non abstract methods


strictfp abstract class Calculator
{
abstract strictfp double addition(double a,double b);//error: illegal combination of modifiers: abstract and strictfp
strictfp double multiplication(double a,double b)
 {
return a*b;
   }
  }

Example 3: Not allowed for variables and constructors


strictfp class StrictfpDemo
{
strictfp int i=10;// error: modifier strictfp not allowed here
strictfp StrictfpDemo// error: modifier strictfp not allowed here 
{
}
public static void main(String[] args)
{
System.out.println("Welcome to Java - Topper Skills");
   }
  }

Share the article to help your friends