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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
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.
Below are the examples of implementing in Strictfp:
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.
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.
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.
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.
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:
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.
The above is the detailed content of Strictfp in Java. For more information, please follow other related articles on the PHP Chinese website!