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();
   }  
 }
 
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章