轉貼來自:http://www.qinglearn.com/article/chengxu/JAVA/info-12099.html
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();
}
為什麼需要使用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:
代碼
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的變數,在Object Storage Service時,這些變數狀態不會被持久化。當對象序列化的儲存在儲存空間上時,不希望有些欄位資料被儲存,為了保證安全性,可以把這些欄位聲明為transient。
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);
}
}