天天看点

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做了升级--重要