Introduction to Strictfp in Java
The primary function of strictfp in Java is to provide the same result on every platform when floating-point variables are in operation. Back in time, when James Gosling and his team were developing java, one of the Language’s main aspects was Platform Independency. Compiling, Interpreting and Executing the same code on different machines and making sure the result is the same and not manipulated by the Processor.
When calculating floating points on various platforms, the results can vary due to the hardware’s capability of processing floating-point by CPU. Strictfp ensures that you get the exact floating-point output on all platforms. With IEEE 754, Standard for Floating-Point Arithmetic, strictfp was introduced in JVM 1.2 version.
For a variety of CPUs, the standard precisions might vary; 32-bit precision is different than of 62-bit and so on for x86 machines. The usage of strictfp makes sure that when the same code is executed on different platforms, the output shall not be manipulated due to different precision, which works fine with greater precision platforms. Understand strictfp as FP Strict, meaning Floating Point Strict.
Syntax in Strictfp
The strictfp is simple and starts with the keyword strictfp. Below are the syntaxes for class, an interface and a method:
1. Class
strictfp class sample1{
// code here will me implicitly strictfp.
}
2. An Interface
strictfp interface sample2 {
// methods here will be implicitly strictfp
}
3. Method
class India {
strictfp void population()
}
In the above samples, we have simply added the keyword strictfp for the class and interface. The above examples demonstrate how strictfp the keyword must be used and now below is an example of where not to use the strictfp keyword.
class sample1{
strictfp float a;
}
Here, assigning a strictfp keyword to the variable won’t work. It shall strictly be passed along with class or an interface.
Example to Implement in Strictfp
Below are the examples of implementing in Strictfp:
Example #1
We will now demonstrate the working of the strictfp keyword by implementing the same in a code example. Below is the program for the same:
Code:
public class Test {
public strictfp double add()
{
double number1 = 10e+10;
double number2 = 6e+08;
return (number1+number2);
}
public static strictfp void main(String[] args)
{
Test example = new Test();
System.out.println(example.add());
}
}
Output:
Explanation to the above code: We started with the class Test definition, then we created a method with a double data type and assigned with it the strictfp keyword. In the add(), we created two variables named number1 and number2 of double data type, and in the next line, the method returns the addition of both these variables. Later we have our main class, which is also strictfp equipped, and the code block inside inherits the strictfp properties. Created a new object of our class, and in the end, we have our output print statement, which simply prints the added value of the two variables.
Example #2
Let us now demonstrate the same strictfp keyword into different programs. Here we won’t do any calculations; instead, we will pass a value in the float variable and print it with a strictfp keyword.
Code:
public class new_strictfp {
float f = 9.381f;
strictfp public void displayValue(){
System.out.println(f);
}
public static void main(String[] args) {
new_strictfp Demo = new new_strictfp ();
Demo.displayValue();
}
}
Output:
Explanation to the above code: This is another example where we have implemented the strictfp keyword. I started with public class and created a new variable with float data type and value. Then created a method to print the float value, and in our main class, we have initialized an object of our class. After the object, we have our output statement, which calls our earlier created method. Here, our method created is strictfp enabled, meaning the floating-point value assigned will be printed as it is.
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. Now let’s understand some rules before we implement the keyword.
- The strictfp keyword cannot be implemented with constructors; it works fine with classes, interfaces, and methods.
- When strictfp is declared with an interface or a class, it will implicitly implement strictfp for all the methods and nested types within.
- strictfp can be implemented with abstract class or interface but not with abstract methods.
- Along with constructors, strictfp cannot be used with variables; refer to the sample code mentioned earlier.
Following the above-mentioned rules will ensure the proper use of strictfp, and the possible difference between floating-point precision giving different results will be avoided.
Advantages of Strictfp in Java
Below are the advantages that come along with the keyword. Advantages of using strictfp are various from a developer’s perspective; a few are listed below:
- Accuracy with Floating Point Result on various machines.
- strictfp harnesses the precision and speed for floating-point operations.
- It is completely unbiased for 32-bit and 64-bit and clearly focuses on better results.
- Lastly, using this keyword as no disadvantage, no increase in time compressibility or execution speed.
- It is important to understand that without the strictfp keyword and its function, the floating-point output can be manipulated according to the target machine’s CPU precision point by the JVM and JIT compiler.
- This is just another better way of ensuring the basics of Java Programming Language, which is “Write Once Run Anywhere”, and not getting different or wrong results everywhere except the source machine.
Conclusion – Strictfp in Java
we understood what a Strictfp Keyword in Java is, then we explored the uses and were not to use this keyword. It was introduced in JVM 1.2 following the Floating Point Standards of IEE 754. We demonstrated the strictfp keyword with syntaxes and later implemented them in examples. It is a modifier that ensures the same results.
Recommended Articles
This is a guide to Strictfp in Java. Here we discuss Syntax to Strictfp, the example to implement with rules to remember and advantages. You can also go through our other related articles to learn more –