JDK1.7新特性

来源:互联网 发布:sai软件如何使用 编辑:程序博客网 时间:2024/06/05 01:09
  • (1) 二进制字面量

  • (2) 数字字面量可以出现下划线

  • (3) switch 语句可以用字符串

  • (4) 泛型简化,菱形泛型

  • (5) 异常的多个catch合并,每个异常用或|

  • (6) try-with-resources 语句

(1) 二进制字面量

System.out.println(0b1111);

(2) 数字字面量可以出现下划线

System.out.println(100_000_000);

(3) switch 语句可以用字符串

  • jdk1.7之前只支持能转换到int类型
switch ("second") {    case "first":        System.out.println("first");        break;    case "second":        System.out.println("second");        break;    case "third":        System.out.println("third");        break;    default:        System.out.println("error");        break;}

(4) 泛型简化,菱形泛型

List<String> list = new ArrayList<String>();// 可以简化为List<String> list = new ArrayList<>();

(5) 异常的多个catch合并,每个异常用或|

  • 仍能识别出异常的具体类型
try {    System.out.println(10 / 0);    System.out.println(arr[10]);   // 数组长度为3} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {    e.printStackTrace();}

(6) try-with-resources 语句

  • JDK1.7新特性-try-with-resources语句
0 0
原创粉丝点击