Is the declaration of the Java class a strict method that it calls in other classes also strictfp?

How is the name really ... if class X is declared strictfp and calls methods in class Y, will strictness be executed, or is this only applicable to X's own code?

Also, if I compute a value in a strictfp class method and pass it to a non-strictfp method, does the value remain "safe" if further calculations are not performed with it?

+6
source share
3 answers

As far as I understand, strictfp limited to the area marked with this keyword. This means that this leads to the fact that the calculations of the marked class or method are carried over.

It cannot extend the effect to the specified code. For example, if foo() is strictfp , but calls bar() from another class that is not strictfp , the calculations inside bar() will not be portable, but the calculations inside foo() will be. So, if the results of bar() are used in foo() , the overall result may not be portable.

 public strictfp double foo() { return bar() * 3.1415926; } public double bar() { return 2.718281828 * 2.0; } 

This result 2.718281828 * 2.0 not portable, but its multiplication by 3.1415926 is.

+4
source

From the Java language specification :

The effect of the strictfp modifier is to make all float or double expressions inside the class declaration (including inside initializers, initializers, static initializers, and constructors) explicitly FP-strict.

[my emphasis]

+2
source

This only applies to expressions in methods declared in this class.

From jGuru article What is strictfp modifier? When will I use it? :

The strictfp can be applied to both classes and methods. For classes (and interfaces), he indicates that all expressions in all methods of the class are FP-strict.

To answer the second question: yes, a floating-point value will be “safe” if it no longer undergoes further calculations, since strictfp applies only to expressions.

Related: When should I use the string "strictfp" keyword in java?

0
source

Source: https://habr.com/ru/post/947051/


All Articles