补发:2019-7-11 学习日记之java基础
try-with-resources statement,即单独使用try块
int month = -1;
try (Scanner sc = new Scanner(System.in)) {
month = sc.nextInt();
}
它会自动关闭括号(recourses)内的资源,不用像
try{
}catch(Exception e){}
那样要手动关闭xx.close();
可定义多个资源。
使用前提:
1、括号内的recourses资源必须实现自java.lang.AutoCloseable接口,该接口只有一个方法:void close();
2、定义和赋值必须都在try里完成