首页 > 编程知识 正文

java中final关键字用法,java中实现接口的关键字

时间:2023-05-04 05:13:41 阅读:110000 作者:1682

Java为什么要引入编写脚本? StrictFP是Java关键字之一,它使用全名strict float point来限制浮点计算,并确保在执行浮点变量操作时在每个平台上获得相同的结果。

浮点计算取决于平台。 这意味着在不同的平台(16/32/64位处理器)上运行类文件可以实现不同的输出(浮点值)。 为了解决这些问题,JDK 1.2版引入了strictfp关键字,以执行符合IEEE754标准的浮点计算。 可能有人不知道IEEE754,但我建议你自己知道~

如果希望浮点运算更准确,并且硬件平台运行的结果不匹配,则建议使用关键字strictfp。

如果strictfp、Java方法strictfp和常规方法的以下代码中的calMoney )由strictfp限定,则表示此方法遵循strictfp的“请求”。

public class coffee { privatedoubleprice; 私有双权重; publiccoffee(doubleprice,double weight ) { this.price=price; this.weight=weight; } public strictfp double calMoney () { return price*weight; } //setters and getters } strictfp和抽象方法strictfp绝对不能用于抽象方法!!!会编译错误的!!!

publicinterfaceicalculate { strictfpdoublemultiply (; }上面的代码使用了strictfp限定接口的抽象方法,编译错误!

如果使用strictfp、Java类strictfp和常规类strictfp修饰符声明类,则该类中声明的所有方法以及在该类中声明的嵌套类型都隐式由strictfp限定。

publicstrictfpclasscoffee { privatedoubleprice; 私有双权重; publiccoffee(doubleprice,double weight ) { this.price=price; this.weight=weight; } public double calMoney () ) { return price*weight; 如} //setters and getters }上的代码所示,calMoney ()没有直接由strictfp限定,但由于Coffee类由strictfp限定,因此实际上calMoney ()是stricttion

strictfp和抽象类strictfp可用于抽象类,继承抽象类并在实现其中的抽象方法时缺省使用strictfp限定。

但是,如上所述,strictfp 不能用于抽象类中抽象方法的修饰,再次强调!

publicabstractstrictfpclassabstractcoffee { privatedoubleprice; publiccoffee(doubleprice ) ) { this.price=price; } public abstract double calculate (); //settersandgetters } publicclasscoffeextendsabstractcoffee { privatedoubleweight; public coffee (双精度,双精度) ) super ) price; this.weight=weight; } public double calculate () { return getPrice ) *weight; } //setters and getters } strictfp和接口strictfp可用于接口,如果实现了接口,并且也实现了其中的抽象方法,则缺省情况下使用strictfp限定。

但是,如上所述,strictfp 不能用于接口中抽象方法的修饰,再次强调!

publicstrictfpinterfaceicalculate { double multiply (; }如上面的代码所示,multiply () (未用strictfp限定,但下面的multiply ) )方法是默认的。

publicclasscoffeeimplementsicalculate { privatedoubleprice; 私有双权重; publiccoffee(doubleprice,double weight ) { this.price=price; this.weight=weight; } public double multiply () { return price*weight; } //setters and getters }上的Coffee类没有用strictfp限定,multiply ()方法也没有用strictfp限定,但multiply ) )是ICalculate接口

在抽象类中,strictfp只能限定抽象方法,普通方法不受限制。 但是,由于接口的方法是隐式抽象的,实际上是strictfp 不能用于接口内的任何方法

strictfp功能测试publicclassstrictfptest { privatestaticstrictfpdoubleadd (floata,double b ) { return a b; } publicstaticvoidmain (string [ ] args ) system.out.println 0.6710339.0415055341984792; system.out.print ln (add (0.6710339 f,0.04150553411984792 ); }测试结果:

0.71253943411984790.712539452974224 strictfp是否高精度? 答案是否定的! 绝对不是!

strictfp只是保证浮点运算严格符合IEEE754,保证各种平台上浮点运算规则的统一,并不能确保高精度。

例如3*0.1。 如果我没记错的话,你用strictfp也不能准确地得到0.3。 更不用说高精细运算了。

高精度运算如果可以使用车轮的话,就使用java.math.BigDecimal吧。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。