1.7新特性
- switch支持string
- 創(chuàng)建泛型實(shí)例,可以通過類型推斷簡(jiǎn)化代碼壁袄,new后面的<>內(nèi)不用再寫泛型
HashMap<String, String> parmas = new HashMap<>();
- try-with-resource語句實(shí)現(xiàn)自動(dòng)資源管理,在try執(zhí)行完畢后自動(dòng)關(guān)閉資源媚媒,關(guān)閉的資源需要實(shí)現(xiàn)java.lang.AutoCloseable接口
private static void customBufferStreamCopy(File source, File target) {
try (InputStream fis = new FileInputStream(source);
OutputStream fos = new FileOutputStream(target)){
byte[] buf = new byte[8192];
int i;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
- 單個(gè)catch捕捉多個(gè)異常嗜逻,異常之間用管道符(|)隔開
public static void testThrows() throws IOException, SQLException {
try {
testThrows();
} catch (IOException | SQLException ex) {
throw ex;
}
}
Java1.8新特性
lambda表達(dá)式,功能接口(只有一個(gè)方法的接口)
接口允許添加非抽象方法缭召,需要添加default字段
public interface Demo {
default public int add(int a,int b){
return a+b;
}
}
- 允許使用::關(guān)子健傳遞方法或者構(gòu)造函數(shù)
還有很多其他新特性栈顷,有時(shí)間再去看吧。哈哈哈