wellnwill Marathahalli Bangalore

strictfp keyword

We may receive different results while performing operations with floating-point variable. We use a strictfp keyword to ensure the same result on every platform(16/32/64 bit processors).

We can use strictfp keyword with

  • class
  • interface
  • method

We cannot use strictfp keyword with

  • abstract method or abstract class
  • variable
  • constructor

strictfp with class

strictfp class Calculator
{   
// if strictfp keyword used with class then all the complete method defined inside class will become strictfp.
}

strictfp with method

class Calculator
{  
    // strictfp complete method 
   //// using strictfp keyword with abstract method is not valid.
    strictfp void add(){}
}

strictfp with interface

strictfp interface Calculator
{   
//if strictfp keyword used with interface then all the method will become strictfp while inheriting in
      sub class.	
}
class Test1 
{
strictfp float f1=7.87f;// strictfp is not allowed with variable.
strictfp Test1(){}   // strictfp is not allowed with constructor.
}
O/P-CTE
strictfp interface Test 
{
    void m1();
    strictfp void m2(); // compile-time error
}

strictfp interface Test 
{
	strictfp default void m3()
        {
	 System.out.println("Hi");
	}
}
class Test1 implements Test
{
public static void main(String a[])
 {
  Test1 t1=new Test1();
  t1.m3();
 }
}
O/P-Hi
strictfp interface Test 
{
	strictfp static void m3()
       {
	 System.out.println("Hi");
       }
}
class Test1 implements Test
{
public static void main(String a[])
 {
  Test.m3();
 }
}
O/P-Hi

strictfp class Test1 
{
   float f1 = 7.87f;
   float f2 = 5.78f;
    
strictfp public void minus()
    {
      System.out.println(f1-f2);
    }
public static void main(String a[])
 {
  Test1 t1 = new Test1();
  t1.minus();

 }
}
Footer with Map