天天看點

jdk7 jdk8 變化

1 二進制變量的表示,支援将整數類型用二進制來表示,用0b開頭

byte aByte = (byte) 0b00100001;

2 數字類型的下劃線表示 更友好的表示方式,不過要注意下劃線添加的一些标準。 不能開頭或結尾

3 Switch語句支援String類型。

4 Try-with-resource語句: 多個try

 try (

 java.util.zip.ZipFile zf =

 new java.util.zip.ZipFile(zipFileName);

 java.io.BufferedWriter writer =

 java.nio.file.Files.newBufferedWriter(outputFilePath, charset)

 ) {

 // Enumerate each entry

 for (java.util.Enumeration entries =

 zf.entries(); entries.hasMoreElements();) {

 // Get the entry name and write it to the output file

 String newLine = System.getProperty("line.separator");

 String zipEntryName =

 ((java.util.zip.ZipEntry)entries.nextElement()).getName() +

 newLine;

 writer.write(zipEntryName, 0, zipEntryName.length());

 }

 }

 BufferedWriter和ZipFile對象的close方法都會自動按聲明的相反順序調用。


 try (Statement stmt = con.createStatement()) {

5 多個catch

catch(IOException | SQLException | Exception ex){



jdk8 

1 接口的預設和靜态方法:

Java 8允許我們給接口添加一個非抽象的方法實作

 public interface JDK8Interface { 


 // static修飾符定義靜态方法 

 static void staticMethod() { 

 System.out.println("接口中的靜态方法"); 

 } 

2 Lambda 表達式:(例如: (x, y) -> { return x + y; } ;λ

public int add(int x, int y) {

 return x + y;

 } 

 變為 (int x, int y) -> x + y;


 c -> { return c.size(); }


3 方法與構造函數引用::

通過Person::new建立一個指向Person構造函數的引用。 請注意構造方法沒有參數

靜态方法:User::collide

類執行個體User::repair


4 支援多重注解

5 日期函數和 Base64

提供了新的java.time包,可以用來替代

java.util.Date和java.util.Calendar。一般會用到Clock、LocaleDate、LocalTime、LocaleDateTime、ZonedDateTime、


6 CurrentHashMap做了更新--重要