Jdk1.7新特性 - 二進制標識、數字間隔符、泛型類型推斷、資源自動管理、多異常處理等

一、二進制字面量標識

    整數類型如(byte,short,int,long)都可以用二進制來表示。表示方法通過在二進制數的前面加入0b或者0B來標示

byte  num1 = 0b00001001;  // 1個字節8位
short num2 = 0b0010000101000101;  // 2個字節16位
int   num3 = 0b10100001010001011010000101000101;  // 4個字節32位
long  num4 = 0b0010000101000101101000010100010110100001010001011010000101000101L; 
 // 8個字節64位

二、數字類型支持間隔符

    使用下劃線(_)將數字分組,以更友好的表達方式提高可讀性。 下劃線不允許出現在 開頭和結尾、0b和0x左右、小數點前後、L和F標識符前。

/**
 * 示例
 */
long number = 1234_5678_9012_3456L;
float price =  3.14_15F;
long hexnum = 0xFF_EC_DE_5E;
long bytes  = 0b11010010_01101001_10010100_10010010;

/**
 * 規則
 */
float f1 = 5_.5F;         // 無效 (不能在小數點前後)
float f2 = 5.2_F;         // 無效 (不能在標識符前)
int x1 = _52;             // 無效 (不能在開頭)
int x2 = 52_;             // 無效 (不能在結尾)
int x3 = 5_2;             // 有效
int x4 = 5_______2;       // 有效 
int x5 = 0_x52;           // 無效 (0x不能拆分)
int x6 = 0x_52;           // 無效 (不能在0x左右)
int x7 = 0x5_2;           // 有效
int x8 = 0x52_;           // 無效 (不能在結尾)
int x9 = 0_52;            // 有效 (八進制)
int x10 = 05_2;           // 有效 (八進制)
int x11 = 052_;           // 無效 (不能在結尾)

三、Switch支持String類型

    在switch的參數中,除了支持byte、short、char、int、long以及包裝類之外,也開始支持將String作爲參數

switch (sw) {
case "N1":
	System.out.println("第一季度");
	break;
case "N2":
	System.out.println("第二季度");
	break;
case "N3":
	System.out.println("第三季度");
	break;
case "N4":
	System.out.println("第四季度");
	break;
default:
	System.out.println("未知");
	break;
}

四、泛型類型實例化類型推斷

    從上下文推斷出參數類型,所以可以用一組空類型參數(<>)替換泛型類構造函數的調用所需的類型參數

/**
 * Jdk 1.7 之前
 */
Map<String, List<String>> oldMap = new HashMap<String, List<String>>();
/**
 * Jdk 1.7 之後
 */
Map<String, List<String>> newMap = new HashMap<>();

五、外部資源支持自動管理

    語句 try-with-resources 是一個用來自動關閉外部資源的語法特性,將外部資源句柄對象的聲明和創建放在 try 關鍵字後面的小括號內即可

    語句 try-with-resources 能夠確保在這個 try-catch 代碼塊執行完畢後,每個資源都會按照聲明的逆序被自動關閉,無需再通過顯示的代碼關閉

    任何實現了Java.lang.AutoCloseable和java.io.Closeable的對象都可以使用 try-with-resource 語句來實現異常處理和關閉資源

    使代碼更精煉和完整,免去了在 finally 中關閉資源的工作和忘記關閉的風險,而 finally 依然可以用來處理一些一定要被執行的代碼塊

    如果對外部資源的處理和對外部資源的關閉均遭遇了異常,“處理異常”將被拋出,而“關閉異常”將被抑制而存放在“處理異常”的被抑制異常列表中

  •     1、Jdk 1.7 之前 的 資源管理
FileReader fr = null;
BufferedReader br = null;
try {
	fr = new FileReader("d:/tst/tst.txt");
	br = new BufferedReader(fr);
	
	String line = null;
	while ((line = br.readLine()) != null) {
		System.out.println(line);
	}
} catch (Exception e) {
	System.out.println(e);
}finally {
	try {
		if(br != null) br.close();
		if(fr != null) fr.close();
	} catch (Exception ex) {
		System.out.println(ex);
	}
}
  •     2、Jdk 1.7 之後 的 資源管理
try (
	FileReader fr =	new FileReader("d:/tst/tst.txt");
	BufferedReader br = new BufferedReader(fr);	
){
	String line = null;
	while ((line = br.readLine()) != null) {
		System.out.println(line);
	}
} catch (Exception e) {
	System.out.println(e);
}

六、一次Catch處理多個異常

    從Jdk1.7開始,一個catch代碼塊可以處理多個異常。如果需要捕獲多個異常並且它們包含相似的代碼,使用這一特性將會減少代碼的重複度

    多個異常之間用管道符(|)將它們分開,在這種情況下異常參數變量(ex)是定義爲final的,所以不能被修改。這一特性將生成更少的字節碼並減少代碼冗餘

  • 1、Jdk 1.7 之前 的 catch處理
try {
	......
}catch (IOException ie) {
	System.out.println(ie);
}catch (SQLException se) {
	System.out.println(se);
}catch (Exception ex) {
	System.out.println(ex);
}
  • 2、Jdk 1.7 之後 的 catch處理
try {
	......
} catch(IOException | SQLException | Exception ex){
    System.out.println(ex);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章