日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JDK7新特性

發布時間:2023/12/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JDK7新特性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JDK7新特性

  • 二進制字面量
  • 在數字字面量使用下劃線
  • Switch中引入String比較類型
  • 泛型推導
  • try-with-resources 資源的自動管理

二進制字面量

整數類型例如(byte,short,int,long)能夠用二進制來表示了。通過在數字前面加入0b或者0B來標示一個二進制的字面量

//一個8位'byte'值: byte aByte = (byte)0b00100001;//一個16位'short'值: short aShort = (short)0b1010000101000101;//一些32位'int'值: int anInt1 = 0b10100001010001011010000101000101; int anInt2 = 0b101; int anInt3 = 0B101; // B可以是大寫也可以是小寫.//一個64位的'long'值. 注意"L"結尾: long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;

參考:https://blog.csdn.net/heartroll/article/details/78455045

在數字字面量使用下劃線

這個功能可以用來對一個數字字面量根據位數分組,從而提高你代碼的可讀性。比如,如果你的代碼包含一些數字有很多的位數,你能夠用下劃線字符把位數分為三組,類似于你用一個像逗號或者一個空格作為分隔符。

下劃線只能出現在數字之間,下面的情形不能出現下劃線:

  • 數字的開頭和結尾
  • 在浮點數中與小數點相鄰
  • F或者L后綴之前
  • 在預期數字串的位置
long creditCardNumber = 1234_5678_9012_3456L; long socialSecurityNumber = 999_99_9999L; float pi = 3.14_15F; long hexBytes = 0xFF_EC_DE_5E; long hexWords = 0xCAFE_BABE; long maxLong = 0x7fff_ffff_ffff_ffffL; byte nybbles = 0b0010_0101; long bytes = 0b11010010_01101001_10010100_10010010;float pi1 = 3_.1415F; // 無效; 不能和小數點相鄰 float pi2 = 3._1415F; // 無效; 不能和小數點相鄰 long socialSecurityNumber1 = 999_99_9999_L; // 無效; 不能放在L后綴之前int x1 = _52; // 無效;這是個標識符,不是數字的字面量 int x2 = 5_2; // OK int x3 = 52_; // 無效; 不能放在數字的結尾 int x4 = 5_______2; // OKint x5 = 0_x52; // 無效; 不能放在 0x 中間 int x6 = 0x_52; // 無效; 不能放在數字的開頭 int x7 = 0x5_2; // OK int x8 = 0x52_; // 無效; 不能放在數字的結尾int x9 = 0_52; // OK int x10 = 05_2; // OK int x11 = 052_; // Invalid; 不能放在數字的結尾

參考:https://blog.csdn.net/heartroll/article/details/78455045

Switch中引入String比較類型

在JDK1.5之前,switch循環只支持byte、short、char、int四種數據類型.
JDK7以前在switch 只支持

  • 基本數據類型:byte, short, char, int
  • 包裝數據類型:Byte, Short, Character, Integer
  • 枚舉類型:Enum

在JDK7 開始支持字符串類型:String

switch語句比較與每個case標簽關聯就好像使用string.equals方法表達的表達式的字符串對象;因此,在switch語句的字符串對象的比較是區分大小寫的。java編譯器生成更有效的字節碼從switch語句中使用字符串對象比鏈式if-then-else語句。

public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {String typeOfDay;switch (dayOfWeekArg) {case "Monday":typeOfDay = "Start of work week";break;case "Tuesday":case "Wednesday":case "Thursday":typeOfDay = "Midweek";break;case "Friday":typeOfDay = "End of work week";break;case "Saturday":case "Sunday":typeOfDay = "Weekend";break;default:throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);}return typeOfDay; }

參考:https://blog.csdn.net/heartroll/article/details/78455045

泛型推導

Map<String, List<String>> myMap = new HashMap<String, List<String>>();//JDK7可以只在申明指定泛型 Map<String, List<String>> myMap = new HashMap<>();

參考:https://blog.csdn.net/heartroll/article/details/78455045

try-with-resources 資源的自動管理

  • 自動關閉資源
    try-with-resources語句是一個聲明一個或多個資源的try語句。一個資源作為一個對象,必須在程序結束之后關閉。try-with-resources語句確保在語句的最后每個資源都被關閉,任何實現了java.lang.AutoCloseable和java.io.Closeable的對象都可以使用try-with-resource來實現異常處理和關閉資源。
  • //JDK1.7之前我們必須在finally塊中手動關閉資源,否則會導致資源的泄露 public class PreJDK7 { public static String readFirstLingFromFile(String path) throws IOException {BufferedReader br = null; try {br = new BufferedReader(new FileReader(path));return br.readLine();} catch (IOException e) {e.printStackTrace();} finally {//必須在這里關閉資源if (br != null)br.close();}return null;} }//JDK1.7之后就可以使用try-with-resources,不需要 public class AboveJDK7 { static String readFirstLineFromFile(String path) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(path))) {return br.readLine();}} }
  • 異常拋出順序
  • 在JDK1.7之前如果rd.readLine()與rd.close()都拋出異常則只會拋出finally塊中的異常,不會拋出rd.readLine()中的異常,這樣經常會導致得到的異常信息不是調用程序想要得到的。

    在JDK1.7及以后采用了try-with-resource機制,如果在try-with-resource聲明中拋出異常(如文件無法打開或無法關閉)的同時rd.readLine()也拋出異常,則只會拋出rd.readLine()的異常。

  • try-with-resource可以聲明多個資源
  • public class AboveJDK7_2 { public static void writeToFileZipFileContents(String zipFileName,String outputFileName) throws java.io.IOException { java.nio.charset.Charset charset = java.nio.charset.Charset.forName("US-ASCII"); java.nio.file.Path outputFilePath = java.nio.file.Paths.get(outputFileName); //打開zip文件,創建輸出流try (java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName); java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset)){//遍歷文件寫入txtfor (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();) { String newLine = System.getProperty("line.separator"); String zipEntryName = ((java.util.zip.ZipEntry) entries.nextElement()).getName() + newLine;writer.write(zipEntryName, 0, zipEntryName.length());}}} }
  • catch 多個exception
  • //befor JDK7 try{ //邏輯代碼 }catch (IOException ex) {logger.log(ex);throw new SpecialException(); catch (SQLException ex) {logger.log(ex);throw new SpecialException(); }//after JDK7 try{ //邏輯代碼 }catch (IOException | SQLException ex) {logger.log(ex);throw new SpecialException(); }
  • Rethrowing Exception with more inclusive Type Checking
  • static class FirstException extends Exception { }static class SecondException extends Exception { }//before JDK7 public void rethrowException(String exceptionName) throws Exception { try { if (exceptionName.equals("First")) {//如果異常名稱為"First",則拋出異常一throw new FirstException(); } else {//否則的話,則拋出異常二throw new SecondException(); } } catch (Exception e) { throw e;} }//after JDK7 public void rethrowException(String exceptionName) throws FirstException, SecondException {// 邏輯代碼同上 }

    try塊中能拋出兩種異常。在java SE7以前的版本中,在方法聲明中throws 只能寫Exception,因為catch里的類型是 Exception。 但是在java SE7及以后的版本中,可以在throws后面寫 FirstException和SecondException——編譯器能判斷出throw e語句拋出的異常e 一定來自try塊,并且try塊只能拋出FirstException和SecondException。
    參考:https://blog.csdn.net/heartroll/article/details/78455045

    總結

    以上是生活随笔為你收集整理的JDK7新特性的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。