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

1.strictfp, 即 strict float point (精确浮点)。
  strictfp 关键字可应用于类、接口或方法。使用 strictfp 关键字声明一个方法时,该方法中所有的float和double表达式都严格遵守FP-strict的限制,符合IEEE-754规范。当对一个类或接口使用 strictfp 关键字时,该类中的所有代码,包括嵌套类型中的初始设定值和代码,都将严格地进行计算。严格约束意味着所有表达式的结果都必须是 IEEE 754 算法对操作数预期的结果,以单精度和双精度格式表示。
  如果你想让你的浮点运算更加精确,而且不会因为不同的硬件平台所执行的结果不一致的话,可以用关键字strictfp. 

保证浮点运算的结果不受平台的影响,在任何平台上,使用统一的标准进行浮点运算,提高程序的可移植性(毕竟结果更可控,更精确),相应的,以降低性能为代价

Strictfp ensures that you get exactly the same results from your floating point calculations on every platform. If you don't use strictfp, the JVM implementation is free to use extra precision where available.

In other words, it's about making sure that Write-Once-Run-Anywhere actually meansWrite-Once-Get-Equally-Wrong-Results-Everywhere.(幽默啊)

With strictfp your results are portable, without it they are more likely to be accurate.


你可以将一个类、接口以及方法声明为strictfp,但是不允许对接口中的方法以及构造函数声明strictfp关键字,例如下面的代码:

1. 合法的使用关键字strictfp

strictfp interface A {}

public strictfp class FpDemo1 {
    strictfp void f() {}
}

2. 错误的使用方法

interface A {
    strictfp void f();
}


public class FpDemo2 {
    strictfp FpDemo2() {}
}

一旦使用了关键字strictfp来声明某个类、接口或者方法时,那么在这个关键字所声明的范围内所有浮点运算都是精确的,符合IEEE-754规范的。例如一个类被声明为strictfp,那么该类中所有的方法都是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();
   }  
 }
 
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值