java中的strictfp关键字

严格的 是一个表示严格浮点的修饰符,它在java 1.2版的基础版本中没有引入。它在java中用于限制浮点计算,并确保在每个平台上执行浮点变量操作时得到相同的结果。 浮点计算取决于平台,即当类文件在不同平台(16/32/64位处理器)上运行时,会实现不同的输出(浮点值)。为了解决这类问题,JDK 1.2版本中引入了strictfp关键字 IEEE 754 浮点计算标准。

null

注: strictfp修饰符仅用于类、接口和方法,但不适用于如下所示的变量:

图1: 关键字在类中的用法

strictfp class Test {       // All concrete methods here are implicitly strictfp.    }

图2: 关键字与接口的使用

strictfp interface Test {       // All  methods here becomes implicitly     // strictfp when used during inheritance.    }class Car {      // strictfp applied on a concrete method     strictfp void calculateSpeed(){}}          

图3: 带变量的关键字用法

strictfp interface Test {    double sum();        // Compile-time error here    strictfp double mul(); }

从以上插图中可以得出以下结论:

  • 使用strictfp修饰符声明类或接口时,类/接口中声明的所有方法以及类中声明的所有嵌套类型都是隐式strictfp。
  • 严格的 不能 与抽象方法结合使用。但是,它可以与抽象类/接口一起使用。
  • 由于接口的方法是隐式抽象的,strictfp不能与接口内的任何方法一起使用。

实例

JAVA

// Java program to illustrate strictfp modifier
// Usage in Classes
// Main class
class GFG {
// Method 1
// Calculating sum using strictfp modifier
public strictfp double sum()
{
double num1 = 10e+ 10 ;
double num2 = 6e+ 08 ;
// Returning the sum
return (num1 + num2);
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating object of class in main() method
GFG t = new GFG();
// Here we have error of putting strictfp and
// error is not found public static void main method
System.out.println(t.sum());
}
}


输出:

图片[1]-java中的strictfp关键字-yiteyi-C++库

本文由 高拉夫·米格拉尼 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享