Java关键字之native,strictfp,transient,volatile

Java关键字(keywords)

  • abstract          default            if                        private              this
    boolean          do                   implements      protected         throw
    break              double            import               public               throws
    byte                 else                instanceof         return               transient
    case                extends          int                      short                 try
    catch               final                 interface          static                 void
    char                 finally              long                  strictfp               volatile
    class               float                native                super                 while
    const               for                   new                   switch
    continue         goto                 package          synchronized

以上是java specifications中定义的keywords,一共48个,其中常见的三个看似是关键字的true, false, null,都不是关键字,而是作为一个单独标识类型。
其中,不常用到的关键字有:const,goto,native,strictfp,transient,volatile。
constgoto为java中的保留字。
1. native
native是方法修饰符。Native方法是由另外一种语言(如c/c++,FORTRAN,汇编)实现的本地方法。因为在外部实现了方法,所以在java代码中,就不需要声明了,有点类似于借口方法。Native可以和其他一些修饰符连用,但是abstract方法和Interface方法不能用native来修饰。
Example:

代码
  1. public interface TestInterface {   
  2.      void doMethod();   
  3. }   
  4. public class Test implements TestInterface {   
  5.     public native void doMethod();   
  6.     private native int doMethodB();   
  7.   public native synchronized String doMethodC();   
  8.   static native void doMethodD();   
  9. }  

为什么需要使用native method?请参考:
http://www.javaeye.com/topic/72543 java Native Method初涉
2. strictfp
修饰类和方法,意思是FP-strict,精确浮点,符合IEEE-754规范的。当一个class或interface用strictfp声明,内部所有的float和double表达式都会成为strictfp的。Interface method不能被声明为strictfp的,class的可以。
Example:
代码
  1. strictfp interface FPTest {   
  2.      void methodA();   
  3. }   
  4. class FPClass implements FPTest {   
  5.     public void methodA() {   
  6.       }   
  7.     public void methodB() {   
  8.   }   
  9.   public strictfp void methodC() {   
  10.   }   
  11. }   
  12. class FPClassB {   
  13.     strictfp void methodA() {   
  14.       }   
  15. }  

3.transient
变量修饰符。标记为transient的变量,在对象存储时,这些变量状态不会被持久化。当对象序列化的保存在存储器上时,不希望有些字段数据被保存,为了保证安全性,可以把这些字段声明为transient。
4. volatile
volatile修饰变量。在每次被线程访问时,都强迫从共享内存中重读该成员变量的值。而且,当成员变量发生变化时,强迫线程将变化值回写到共享内存。这样在任何时刻,两个不同的线程总是看到某个成员变量的同一个值。
看看Java Language Specification中的例子。
条件:一个线程不停的调用方法one(),一个线程不停的调用方法two()。我测试过多次,这种情况好像一直没有出现。
代码
  1. class Test {   
  2.     static int i = 0, j = 0;   
  3.     static void one() { i++; j++; }   
  4.     static void two() {   
  5.           System.out.println("i=" + i + " j=" + j);   
  6.       }   
  7. }  

结果偶尔会出现j大于i的情况,因为方法没有同步,所以会出现i和j可能不是一次更新。一种防止这种情况发生的办法就是声明两个方法为synchronized 的。
代码
  1. class Test {   
  2.     static int i = 0, j = 0;   
  3.     static synchronized void one() { i++; j++; }   
  4.     static synchronized void two() {   
  5.           System.out.println("i=" + i + " j=" + j);   
  6.       }   
  7. }  

这样可以防止两个方法同时被执行,还可以保证j和i被同时更新,这样一来i和j的值一直是一样的。
另外一种途径就是把i和j声明为volatile。
代码
  1. class Test {   
  2.     static volatile int i = 0, j = 0;   
  3.     static void one() { i++; j++; }   
  4.     static void two() {   
  5.           System.out.println("i=" + i + " j=" + j);   
  6.       }   
  7. }  

http://hi.baidu.com/liyunqi1984/blog/item/0128921373d98b015aaf53eb.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值