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。
const和goto为java中的保留字。

1. native
native是方法修饰符。Native方法是由另外一种语言(如c/c++,FORTRAN,汇编)实现的本地方法。因为在外部实现了方法,所以在 java代码中,就不需要声明了,有点类似于借口方法。Native可以和其他一些修饰符连用,但是abstract方法和Interface方法不能用 native来修饰。
Example:
public   interface  TestInterface  {
    
void doMethod();
}

public   class  Test  implements  TestInterface  {
    
public native void doMethod();
    
private native int doMethodB();
 
public native synchronized String doMethodC();
 
static native void doMethodD();
}


2. strictfp
修饰类和方法,意思是FP-strict,精确浮点,符合IEEE-754规范的。当一个class或interface用strictfp声明,内部所有的float和double表达式都会成为strictfp的。Interface method不能被声明为strictfp的,class的可以。

strictfp   interface  FPTest  {
    
void methodA();
}

class  FPClass  implements  FPTest  {
    
public void methodA() {
    }

    
public void methodB() {
  }

 
public strictfp void methodC() {
  }

}

class  FPClassB  {
    
strictfp void methodA() {
    }

}


3.transient
变量修饰符。标记为transient的变量,在对象存储时,这些变量状态不会被持久化。当对象序列化的保存在存储器上时,不希望有些字段数据被保存,例如敏感性数据像是密码等,为了保证安全性,可以把这些字段声明为transient。
    当一个对象被串行化的时候,transient型变量的值不包括在串行化的表示中,然而非transient型的变量是被包括进去的。当串行化某个对象时,如果该对象的某个变量是transient,那么这个变量不会被串行化进去。也就是说,假设某个类的成员变量是transient,那么当通过 ObjectOutputStream把这个类的某个实例保存到磁盘上时,实际上transient变量的值是不会保存的。
import  java.util. * ;
import  java.io. * ;

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:     " + "user: " + uid + "    logging date : " + loggingDate.toString() + "    password: " + password;
    }


    
public static void main(String args[]) {
        LoggingInfo logInfo 
= new LoggingInfo("MIKE""MECHANICS");
        System.out.println(logInfo.toString());
        
try {
            ObjectOutputStream o 
= new ObjectOutputStream(new FileOutputStream("logInfo.out"));
            o.writeObject(logInfo);
            o.close();
        }

        
catch (Exception e) {
        }


        
try {
            ObjectInputStream in 
= new ObjectInputStream(new FileInputStream("logInfo.out"));
            LoggingInfo logInfo1 
= (LoggingInfo) in.readObject();
            System.out.println(logInfo1.toString());
        }

        
catch (Exception e) {
        }

    }

}

OUTPUT:
logon info:
   user: MIKE
   logging date : Tue Jan 08 13:44:04 CST 2008
   password: MECHANICS
logon info:
   user: MIKE
   logging date : Tue Jan 08 13:44:04 CST 2008
   password: NOT SET

 4. volatile
volatile修饰变量。在每次被线程访问时,都强迫从共享内存中重读该成员变量的值。而且,当成员变量发生变化时,强迫线程将变化值回写到共享内存。这样在任何时刻,两个不同的线程总是看到某个成员变量的同一个值。
看看Java Language Specification中的例子。
条件:一个线程不停的调用方法one(),一个线程不停的调用方法two()。我测试过多次,这种情况好像一直没有出现。
class  Test  {
    
static int i = 0, j = 0;
    
static void one() { i++; j++; }
    
static void two() {
        System.out.println(
"i=" + i + " j=" + j);
    }

}

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

}

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

}

你可能感兴趣的:(Java关键字之native,strictfp,transient,volatile)