Strictfp Introduction

  • strictfp is a Java keyword that restricts floating-point calculations in order to ensure portability
  • The modifier was added to the Java programming language with the Java virtual machine version 1.2.
  • Older versions of JVMs were always use strict floating-point, means all values used during floating-point calculations are made in the IEEE-standard float or double sizes.
  • We can also use strictfp keyword on methods, classes and interfaces
  • Strictfp with Method
    class Test{
    strictfp void showValue(){}
    }
    
    Strictfp with Class
    strictfp class Test{}	
    
    Strictfp with Interface
    strictfp interface Test{}
    
Some strictfp expression not allowed in java
class Test{
	strictfp abstract void showValue(){}
}
		
class Test{
	strictfp int salary=20000;
}
		
class Test{
	strictfp sum(){}
}
		

Example
public class StrictFP 
{ 
	public static strictfp void main(String[] args) 
	{ 
		double alpha = 8e+307;
		System.out.println(mulNotStrict(alpha));
		System.out.println(mulStrictFP(alpha)); 
		System.out.println(2 * alpha); 
	} 
	static double mulNotStrict(double a) 
	{ 
		return a * 4 * 0.5;
	} 
	static strictfp double mulStrictFP(double a) 
	{ 
		return a * 4 * 0.5;
	} 
}