JDK7 新特性

概述

以下是JDK7的重要特性:

  • 支持二进制变量的表示
  • 支持数字类型的下划线表示
  • Switch语句支持String类型
  • 泛型简化,菱形泛型
  • Catch支持多个异常
  • Try-with-resource语句, 1.7版标准的异常处理代码
  • 简化了可变参数方法的调用

新特性

  • 二进制变量的表示,支持将整数类型用二进制来表示, 所有整数类型 int、short、long、byte都可以用二进制表示,表示的方法就是在二进制数字前面加上0b
@Test
public void binaryTest() {
    byte num1 = 0b00001001;  //1个字节8位
    short num2 = 0b0010000101000101; //2个字节16位
    int num3 = 0b10100001010001011010000101000101;//4个字节32位
    long num4 = 0b0010000101000101101000010100010110100001010001011010000101000101L;//8个字节64位

    System.out.println(num1); // 9
    System.out.println(num2); // 8517
    System.out.println(num3); // -1589272251
    System.out.println(num4); // 2397499697075167557
}
  • 支持数字类型的下划线表示
@Test
public void numberUnderlineTest() {
    int one_million = 1_000_000; // int 数学下划线
    int binary = 0b1001_1001; // int 二进制
    long long_num = 1_00_00_000; // long型
    float float_num = 2.10_001F; // float型
    double double_num = 2.10_12_001; // double型

    System.out.println(one_million); // 1000000
    System.out.println(binary); // 153
    System.out.println(long_num); // 10000000
    System.out.println(float_num); // 2.10001
    System.out.println(double_num); // 2.1012001
}
  • Switch语句支持String类型。在1.6的版本switch的参数中只支持byte、short、char、int、long以及他们的包装类(自动拆箱和自动装箱的支持下),然后在jdk1.7支持了String作为参数。
@Test
public void switchStrTest() {
    String str = "Switch.Two";
    switch (str) {
        case "Switch.One":
            System.out.println("Switch.One");
            break;
        case "Switch.Two":
            System.out.println("Switch.Two");
            break;
        case "Switch.Three":
            System.out.println("Switch.Three");
            break;
        default:
            System.out.println("Switch.Other");
    }
}

// 打印 Switch.Two
  • 泛型简化,菱形泛型。可以去掉后面new部分的泛型类型,只用<>

新特性之前:

List list = new ArrayList();
List<String> list2 = new ArrayList<String>();
List<Map<String, List<String>>> list3 =  new ArrayList<Map<String, List<String>>>();

新特性:

List list = new ArrayList();
List<String> list2 = new ArrayList<>();
List<Map<String, List<String>>> list3 =  new ArrayList<>();
  • Catch支持多个异常
@Test
public void throwsTest() throws Exception{
    try {
        IOExceptionTest();
        SQLExceptionTest();
    } catch (IOException | SQLException ex) {
        throw ex;
    }
}

public void IOExceptionTest() throws IOException {
    throw new IOException("This is an IOException.");
}

public void SQLExceptionTest() throws SQLException {
    throw new SQLException("This is an SQLException.");
}
  • Try-with-resource语句, 1.7版标准的异常处理代码

JDK7之后,Java多了个新的语法:try-with-resources语句,可以理解为是一个声明一个或多个资源的 try语句(用分号隔开),一个资源作为一个对象,并且这个资源必须要在执行完关闭的,try-with-resources语句确保在语句执行完毕后,每个资源都被自动关闭 。任何实现了** java.lang.AutoCloseable**的对象, 包括所有实现了 java.io.Closeable 的对象,都可以用作一个资源。

模拟测试自定义AutoCloseable

/**
 * 自定义AutoCloseable
 *
 * @author Benji
 * @date 2019-07-02
 */
public class MyAutoClosable implements AutoCloseable {

    public void doIt() {
        System.out.println("MyAutoClosable doing it!");
    }

    @Override
    public void close() throws Exception {
        System.out.println("MyAutoClosable closed!");
    }

}

测试

@Test
public void autoClosableTest () {
    try(MyAutoClosable myAutoClosable = new MyAutoClosable()){
        myAutoClosable.doIt();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // 执行
    // MyAutoClosable doing it!
    // MyAutoClosable closed!
}

新特性前,需要在最后finally中关闭读文件流。

public void readFile1() throws FileNotFoundException {
    FileReader fr = null;
    BufferedReader br = null;
    try {
        fr = new FileReader("/Users/zhengweilu/input.txt");
        br = new BufferedReader(fr);
        String s = "";
        while ((s = br.readLine()) != null) {
            System.out.println(s);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

新特性,try with resource写法

public void readFile() throws FileNotFoundException {
    try (
            FileReader fr = new FileReader("/Users/zhengweilu/input.txt");
            BufferedReader br = new BufferedReader(fr)
    ) {
        String s = "";
        while ((s = br.readLine()) != null) {
            System.out.println(s);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • 简化了可变参数方法的调用

当程序员试图使用一个不可具体化的可变参数并调用一个varargs (可变)方法时,编辑器会生成一个“非安全操作”的警告。

小栗子

我丢工作了,可恶,被上面的人抢了饭碗...

上次更新: 2020-5-10 1:34:13 PM