1.strictfp, 即 strict float point (精确浮点)。

  strictfp keyword可应用于类、接口或方法。使用 strictfp keyword声明一个方法时,该方法中全部的float和double表达式都严格遵守FP-strict的限制,符合IEEE-754规范。当对一个类或接口使用 strictfp keyword时,该类中的全部代码,包含嵌套类型中的初始设定值和代码,都将严格地进行计算。严格约束意味着全部表达式的结果都必须是 IEEE 754 算法对操作数预期的结果,以单精度和双精度格式表示。

  假设你想让你的浮点运算更加精确,并且不会由于不同的硬件平台所运行的结果不一致的话,能够用keywordstrictfp.

演示样例 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中不常见的keyword:strictfp,transient的更多相关文章

  1. JAVA中字符串常见操作

    String str1="hello,world";String str2="Hello,World"; 1.字符串的比较:例,System.out.print ...

  2. java中不常见的关键字:strictfp

    1.strictfp, 即 strict float point (精确浮点). strictfp 关键字可应用于类.接口或方法.使用 strictfp 关键字声明一个方法时,该方法中所有的float ...

  3. Java 中最常见的 5 个错误

    在编程时,开发者经常会遭遇各式各样莫名错误.近日,Sushil Das 在 Geek On Java上列举了 Java 开发中常见的 5 个错误,与君共「免」. 原文链接:Top 5 Common M ...

  4. Java学习之道:Java中十个常见的违规编码

    近期,我给Java项目做了一次代码清理工作.经过清理后,我发现一组常见的违规代码(指不规范的代码并不表示代码错误)反复出如今代码中.因此,我把常见的这些违规编码总结成一份列表,分享给大家以帮助Java ...

  5. Java中十个常见的违规编码

    摘要:作者Veera Sundar在清理代码工作时发现一些常见的违规编码,因此,Veera Sundar把针对常见的一些违规编码总结成一份列表,以便帮助Java爱好者提高代码的质量和可维护性. 最近, ...

  6. java中各种常见的异常

    一.各种常见的异常 在上一节中程序如果你注意留意,程序抛出的异常是:java.lang.ArithmeticException.这个异常是在lang包中已经定义的.在lang包中还定义了一些我们非常常 ...

  7. Java中部分常见语法糖

    语法糖(Syntactic Sugar),也称糖衣语法,指在计算机语言中添加的某种语法,这种语法对语言本身功能来说没有什么影响,只是为了方便程序员的开发,提高开发效率.说白了,语法糖就是对现有语法的一 ...

  8. Java中最常见的十道面试题

    第一,谈谈final, finally, finalize的区别. final?修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此一个类不能既被声明为 ...

  9. Java注解【二、Java中的常见注解】

    JDK自带注解 @Override 重写 @Deprecated 已过期 @Suppvisewarnings 压制警告 Demo: public interface Person { public S ...

随机推荐

  1. java沙箱机制原理

    参考文档如下: http://www.2cto.com/kf/201012/79578.html

  2. 纯 CSS 创建各种不同的图形形状

    使用代码 矩形 .rectangle { width: 250px; height: 150px; background-color: #6DC75F; } <div></div&g ...

  3. BestCoder Round 59 (HDOJ 5500) Reorder the Books

    Problem Description dxy has a collection of a series of books called “The Stories of SDOI”,There are ...

  4. Cacti添加Advance Ping监控模板

    Cacti脚本   1.Advance Ping 脚本及模板   注:要使用此模板,编译PHP时必须加上--enable-sockets选项来支持套接字.   1).功能:此模板用来监控一个TCP/U ...

  5. HashMap的扩容机制, ConcurrentHashMap和Hashtable主要区别

    源代码查看,有三个常量, static final int DEFAULT_INITIAL_CAPACITY = 16; static final int MAXIMUM_CAPACITY = 1 & ...

  6. PHP中的定界符格式

    <?php //nowdoc(单引号定界符) //ABC可以是任合内容,放在单引号中 $c=<<<'ABC' 这里可以是任合内容 我是历的苛夺基 本原则叶落归根在运 输费艰难田 ...

  7. oracle 存储过程,函数和包

    创建存储过程: 语法:create [or replace] PROCEDURE 过程名(参数列表)  AS PLSQL子程序体: 调用 存储过程的方式 两种1.execute(exec)     - ...

  8. STL-空间配置器(allocator)

    STL的空间配置器作为STL六大部件的重要组成部分,它总是隐藏在一切组件的背后.它主要负责动态空间的分配.释放等管理工作.整个STL的操作对象(所有的数值)都存放在容器之内,而容器一定需要配置空间以置 ...

  9. Memcached使用手册

    memcached简介 1.memcached是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库检索的结果 ...

  10. svn用法

    SVN安装与启动服务 1.下载相应匹配版本的TortoiseSVN(客户端)和Subversion(客户端),安装完毕之后. 2.配置相关文件. 选择TortoiseSVN-->create r ...