JDK7在語法上的幾處小變化

1 ,菱形語法(泛型實例化類型自動推斷)

 

Java代碼
  1. List<String> list =  new  ArrayList<>();  // <>這個真的很像菱形   
List<String> list = new ArrayList<>(); // <>這個真的很像菱形
 

2 ,在目前版本中,不可具體化的泛型(任意類型)可變參數,在編譯時,會在調用處產生警告, JDK7 裏將這個警告挪到了方法定義處。

變化前:

 

Java代碼
  1. static  <T> List<T> asList(T... elements) { ... }  
  2. static  List<Callable<String>> stringFactories() {  
  3.     Callable<String> a, b, c;  
  4.     ...  
  5.      // 警告處   
  6.     return  asList(a, b, c);  
  7.   }  
static <T> List<T> asList(T... elements) { ... }
static List<Callable<String>> stringFactories() {
    Callable<String> a, b, c;
    ...
     // 警告處
    return asList(a, b, c);
  }
 

變化後:

 

Java代碼
  1. // 警告處   
  2. static  <T> List<T> asList(T... elements) { ... }  
  3. static  List<Callable<String>> stringFactories() {  
  4.     Callable<String> a, b, c;  
  5.     ...  
  6.     return  asList(a, b, c);  
  7.   }  
// 警告處
static <T> List<T> asList(T... elements) { ... }
static List<Callable<String>> stringFactories() {
    Callable<String> a, b, c;
    ...
    return asList(a, b, c);
  }
 

3 ,字符串終於可以 switch .

 

Java代碼
  1. String s = ...  
  2. switch (s) {  
  3.   case   "quux" :  
  4.     processQuux(s); //沒有break,繼續往下   
  5.   
  6.   case   "foo" :  
  7.   case   "bar" :  
  8.     processFooOrBar(s);  
  9.     break ;  
  10.   case   "baz" :  
  11.      processBaz(s); //沒有break,繼續往下   
  12.   
  13.   default :  
  14.     processDefault(s);  
  15.     break ;  
  16. }  
String s = ...
switch(s) {
  case "quux":
    processQuux(s); //沒有break,繼續往下

  case "foo":
  case "bar":
    processFooOrBar(s);
    break;
  case "baz":
     processBaz(s); //沒有break,繼續往下

  default:
    processDefault(s);
    break;
}
 

4 ,支持二進制語法和單位級別的數字表示方式

 

Java代碼
  1. // 8位byte   
  2. byte  aByte = ( byte )0b00100001;  
  3. // 16位short   
  4. short  aShort = ( short )0b1010000101000101;  
  5. // 32位int   
  6. int  anInt1 = 0b10100001010001011010000101000101;  
// 8位byte
byte aByte = (byte)0b00100001;
// 16位short
short aShort = (short)0b1010000101000101;
// 32位int
int anInt1 = 0b10100001010001011010000101000101;
 

支持單位級別的數字,提高可讀性

 

Java代碼
  1. long  underScores = 9_223_372_036_854_775_807L;  // 每三位加一下劃線,等同於 9,223,372,036,854,775,807   
long underScores = 9_223_372_036_854_775_807L; // 每三位加一下劃線,等同於 9,223,372,036,854,775,807
 

5 ,從語法層面上支持集合,不再是數組的專利。

 

Java代碼
  1. final  List<Integer> piDigits = [ 3 1 4 1 5 9 2 6 5 3 5 9 ];  
  2. final  Set<Integer> primes = {  2 7 31 127 8191 131071 524287  };  
  3. final  Map<Integer, String> platonicSolids = {  4  :  "tetrahedron" ,  
  4. 6  :  "cube" 8  :  "octahedron" 12  :  "dodecahedron" 20  :  "icosahedron"   
  5. };  
final List<Integer> piDigits = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9];
final Set<Integer> primes = { 2, 7, 31, 127, 8191, 131071, 524287 };
final Map<Integer, String> platonicSolids = { 4 : "tetrahedron",
6 : "cube", 8 : "octahedron", 12 : "dodecahedron", 20 : "icosahedron"
};
 

6 JSR 292 動態類型語言支持

 

Java代碼
  1. Dynamic x = (動態語言腳本);  
  2. Object  y = x.foo("ABC" ).bar( 42 ).baz();  
Dynamic x = (動態語言腳本);
Object  y = x.foo("ABC").bar(42).baz();
 

7 ,動態資源管理

在目前版本的 java 中,當你操作流時,一定會加 try..finally 以保證出現異常時,流能被正確關閉。

 

Java代碼
  1. BufferedReader br =  new  BufferedReader( new  FileReader(path));  
  2. try  {  
  3.     return  br.readLine();  
  4. finally  {  
  5.     br.close();  
  6. }  
BufferedReader br = new BufferedReader(new FileReader(path));
try {
    return br.readLine();
} finally {
    br.close();
}
 

JDK7 裏,你只需要將資源定義在 try() 裏, Java7 就會在 readLine 拋異常時,自動關閉資源。

另外,資源類必須實現 Disposable<?> 接口。支持管理多個資源。

 

Java代碼
  1. try  (BufferedReader br =  new  BufferedReader( new  FileReader(path)) {  
  2.     return  br.readLine();  
  3. }  
try (BufferedReader br = new BufferedReader(new FileReader(path)) {
    return br.readLine();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章