更简洁的 try-with-resources 语句
现在可以把变量声明在外面,try-with-resources语句里只写引用了
Scanner s = new Scanner(System.in);
// try(Scanner s = new Scanner(System.in))
try (s) {
s.next();
} catch (Exception e) {
e.printStackTrace();
}
_ 不再是一个合法的变量名
java9不允许以一条下划线作为变量名
int _ = 1; // error
接口中允许定义private方法
不能是abstract的
interface Inter {
private void method(){
}
}
这是为了让非abstract方法间可以复用代码
<> 可以与匿名内部类一起使用了
就像这样
// Comparator<Integer> c = new Comparator<Integer>() {
Comparator<Integer> c = new Comparator<>() {
@Override
public int compare(Integer o1, Integer o2) {
return 0;
}
};
@SafeVarargs 允许用在私有实例方法中
class C<T>{
@SafeVarargs
private void method(T ...s){
}
}
本文介绍了Java9中的一些重要更新,包括更简洁的try-with-resources语法、接口中私有方法的支持、类型推断在匿名内部类中的应用及@SafeVarargs注解的新用途。
1240

被折叠的 条评论
为什么被折叠?



