JDK7新特性

JDK7 特性列表

1.switch 中添加对 String 类型的支持
2.数字字面量的改进 / 数值可加下划
3.异常处理(捕获多个异常) try-with-resources
4.增强泛型推断
5.JSR203 NIO2.0(AIO)新 IO 的支持
6.JSR292 与 InvokeDynamic 指令
7.Path 接口、DirectoryStream、Files、WatchService(重要接口更新)
8.fork/join framework

1.switch 中添加对 String 类型的支持
public class SwitchTest {
    public static void main(String[] args) {
        String gender = "男";
        switch (gender) {
            case "男":
                System.out.println(gender);
                break;
            case "女":
                System.out.println(gender);
            default:
                System.out.println("其他");
                break;
        }
    }
}

注意:
编译器在编译时先做处理:
①case 仅仅有一种情况。直接转成 if。
②假设仅仅有一个 case 和 default,则直接转换为 if…else…。
③有多个 case。先将 String 转换为 hashCode,然后相应的进行处理,JavaCode在底层兼容 Java7 曾经版本号。

2.数字字面量的改进 / 数值可加下划

Java7 前支持十进制(123)、八进制(0123)、十六进制(0X12AB)
Java7 添加二进制表示(0B11110001、0b11110001)
数字中可加入分隔符

Java7 中支持在数字量中间添加’_'作为分隔符。更直观,如(12_123_456)。
下划线仅仅能在数字中间。编译时编译器自己主动删除数字中的下划线。

public class SwitchTest {
    public static void main(String[] args) {
       int money = 123_146_454;
        System.out.println(money);
    }
}

打印结果

123146454
3.异常处理(捕获多个异常) try-with-resources
public class SwitchTest {
    public static void main(String[] args) {
        FileInputStream fis = null;
      try {
          fis = new FileInputStream(new File(""));
          Class clazz = Class.forName("");
      }catch (IOException e){
          e.printStackTrace();
      }catch (ClassNotFoundException e) {
        e.printStackTrace();
      }finally {
          try {
              fis.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
    }
}

try-with-resources 语句
Java7 之前须要在 finally 中关闭 socket、文件、数据库连接等资源;
Java7 中在 try 语句中申请资源,实现资源的自己主动释放(资源类必须实现java.lang.AutoCloseable 接口,一般的文件、数据库连接等均已实现该接口,close 方法将被自己主动调用)

public class SwitchTest {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream(new File(""))) {
            System.out.println(fis);
            Class clazz = Class.forName("");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
public class FileInputStream extends InputStream{}
public abstract class InputStream implements Closeable {}
public interface Closeable extends AutoCloseable {}
4.增强泛型推断
Map> map = new HashMap>()

JDK7之后

Map> anagrams = new HashMap<>();
5.JSR203 NIO2.0(AIO)新 IO 的支持

bytebuffer

public class ByteBufferUsage {
    public void useByteBuffer() {
        ByteBuffer buffer = ByteBuffer.allocate(32);
        buffer.put((byte)1);
        buffer.put(new byte[3]);
        buffer.putChar('A');
        buffer.putFloat(0.0f);
        buffer.putLong(10, 100L);
        System.out.println(buffer.getChar(4));
        System.out.println(buffer.remaining());
    }
    public void byteOrder() {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(1);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        //值为 16777216
        buffer.getInt(0);
    }
    public void compact() {
        ByteBuffer buffer = ByteBuffer.allocate(32);
        buffer.put(new byte[16]);
        buffer.flip();
        buffer.getInt();
        buffer.compact();
        int pos = buffer.position();
    }
    public void viewBuffer() {
        ByteBuffer buffer = ByteBuffer.allocate(32);
        buffer.putInt(1);
        IntBuffer intBuffer = buffer.asIntBuffer();
        intBuffer.put(2);
        //值为 2
        int value = buffer.getInt();
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ByteBufferUsage bbu = new ByteBufferUsage();
        bbu.useByteBuffer();
        bbu.byteOrder();
        bbu.compact();
        bbu.viewBuffer();
    }
}

filechannel

public class FileChannelUsage {
    public void openAndWrite() throws IOException {
        FileChannel channel = FileChannel.open(Paths.get("my.txt"),
                StandardOpenOption.CREATE, StandardOpenOption.WRITE);
        ByteBuffer buffer = ByteBuffer.allocate(64);
        buffer.putChar('A').flip();
        channel.write(buffer);
    }

    public void readWriteAbsolute() throws IOException {
        FileChannel channel =
                FileChannel.open(Paths.get("absolute.txt"),
                        StandardOpenOption.READ, StandardOpenOption.CREATE,
                        StandardOpenOption.WRITE);
        ByteBuffer writeBuffer =
                ByteBuffer.allocate(4).putChar('A').putChar('B');
        writeBuffer.flip();
        channel.write(writeBuffer, 1024);
        ByteBuffer readBuffer = ByteBuffer.allocate(2);
        channel.read(readBuffer, 1026);
        readBuffer.flip();
        //值为'B'
        char result = readBuffer.getChar();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        FileChannelUsage fcu = new FileChannelUsage();
        fcu.openAndWrite();
        fcu.readWriteAbsolute();
    }
}

具体NIO2会拿出来单独讲解

6.JSR292 与 InvokeDynamic 指令

JSR 292: Supporting Dynamically Typed Languages on the JavaTM Platform,支持在 JVM 上运行动态类型语言。在字节码层面支持了 InvokeDynamic

7.Path 接口、DirectoryStream、Files、WatchService(重要接口更新)

Path

8.fork/join framework

具体参见博客 @jdk7和jdk8的一些新特性及区别

你可能感兴趣的:(JDK7新特性)