日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 中不常见的关键字:strictfp,transient

發布時間:2024/4/17 编程问答 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 中不常见的关键字:strictfp,transient 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.strictfp, 即 strict float point (精確浮點)。
  strictfp 關鍵字可應用于類、接口或方法。使用 strictfp 關鍵字聲明一個方法時,該方法中所有的float和double表達式都嚴格遵守FP-strict的限制,符合IEEE-754規范。當對一個類或接口使用 strictfp 關鍵字時,該類中的所有代碼,包括嵌套類型中的初始設定值和代碼,都將嚴格地進行計算。嚴格約束意味著所有表達式的結果都必須是 IEEE 754 算法對操作數預期的結果,以單精度和雙精度格式表示。
  如果你想讓你的浮點運算更加精確,而且不會因為不同的硬件平臺所執行的結果不一致的話,可以用關鍵字strictfp.?
示例 1?
  下面的示例演示了一個使用 strictfp 修飾符聲明的類。?
Java代碼??
package com.magical;???
??
// Example of precision control with strictfp???
public strictfp class MyClass {???
??? public static void main(String[] args)???
??? {???
??????? float aFloat = 0.6710339f;???
??????? double aDouble = 0.04150553411984792d;???
??????? double sum = aFloat + aDouble;???
??????? float quotient = (float)(aFloat / aDouble);???
??????? System.out.println("float: " + aFloat);???
??????? System.out.println("double: " + aDouble);???
??????? System.out.println("sum: " + sum);???
??????? System.out.println("quotient: " + quotient);???
??? }???
}?

package com.magical;

// Example of precision control with strictfp
public strictfp class MyClass {
?public static void main(String[] args)
?{
??float aFloat = 0.6710339f;
??double aDouble = 0.04150553411984792d;
??double sum = aFloat + aDouble;
??float quotient = (float)(aFloat / aDouble);
??System.out.println("float: " + aFloat);
??System.out.println("double: " + aDouble);
??System.out.println("sum: " + sum);
??System.out.println("quotient: " + quotient);
?}
}


運行結果:?
float: 0.6710339?
double: 0.04150553411984792?
sum: 0.7125394529774224?
quotient: 16.167336

?

2.transient

當串行化某個對象時,如果該對象的某個變量是transient,那么這個變量不會被串行化進去。也就是說,假設某個類的成員變量是transient,那么當通過

ObjectOutputStream把這個類的某個實例

保存到磁盤上時,實際上transient變量的值是不會保存的。因為當從磁盤中讀出這個對象的時候,對象的該變量會沒有被賦值。

??? 另外這篇文章還提到,當從磁盤中讀出某個類的實例時,實際上并不會執行這個類的構造函數,而是讀取這個類的實例的狀態,并且把這個狀態付給這個類的對象。

?

?

? import java.util.*;
public class LoggingInfo implements java.io.Serializable

{

private Date loggingDate = new Date();

private String uid;

private transient String pwd;

LoggingInfo(String user, String password)

{

uid = user;

pwd = password;

}

public String toString()

{

String password=null;

if(pwd == null)

{

password = "NOT SET";

}

else

{

password = pwd;

}

return "logon info: \n " + "user: " + uid +

"\n logging date : " + loggingDate.toString() +

"\n password: " + password;

}

}

?

import java.io.*;
public class Serializable{
?
?
?public static? void main(String args[]){
??
??
??
??
? LoggingInfo logInfo = new LoggingInfo("小徐", "不知道");

? System.out.println(logInfo.toString());

? try

? {

? ObjectOutputStream o = new ObjectOutputStream(

? new FileOutputStream("logInfo.out"));

? o.writeObject(logInfo);

? o.close();

? }

? catch(Exception e) {//deal with exception
??
? e.printStackTrace();
? }

?// To read the object back, we can write

? try

? {

? ObjectInputStream in =new ObjectInputStream(

? new FileInputStream("logInfo.out"));

? LoggingInfo logInfo1 = (LoggingInfo)in.readObject();

? System.out.println(logInfo1.toString());?
?
? }?
?
? catch(Exception e)
?? {//deal with exception
??? e.printStackTrace();
?? }?
??
?}
}

?

import java.util.*;
public class LoggingInfo_ implements java.io.Serializable

{

private Date loggingDate = new Date();

private String uid;

private transient String pwd;

public? LoggingInfo_()

{

this.uid = "小徐";

this.pwd = "不知道";

}

public String toString()

{

String password=null;

if(pwd == null)

{

password = "NOT SET";

}

else

{

password = pwd;

}

return "logon info: \n " + "user: " + uid +

"\n logging date : " + loggingDate.toString() +

"\n password: " + password;

}

}

?

?

import java.io.*;
public class Serializable_{
?
?
?public static? void main(String args[]){
??
??
? LoggingInfo_ logInfo_ = new LoggingInfo_();

? System.out.println(logInfo_.toString());

? try

? {

? ObjectOutputStream o = new ObjectOutputStream(

? new FileOutputStream("logInfo_.out"));

? o.writeObject(logInfo_);

? o.close();

? }

? catch(Exception e) {//deal with exception
??
? e.printStackTrace();
? }

?// To read the object back, we can write

? try

? {

? ObjectInputStream in =new ObjectInputStream(

? new FileInputStream("logInfo_.out"));

? LoggingInfo_ logInfo_1 = (LoggingInfo_)in.readObject();

? System.out.println(logInfo_1.toString());?
?
? }?
?
? catch(Exception e)
?? {//deal with exception
??? e.printStackTrace();
?? }??
?}
?
}

總結

以上是生活随笔為你收集整理的java 中不常见的关键字:strictfp,transient的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。