特性列表
switch中添加对String类型的支持
数字字面量的改进 / 数值可加下划
异常处理(捕获多个异常) try-with-resources
增强泛型推断
JSR203 NIO2.0(AIO)新IO的支持
JSR292与InvokeDynamic指令
Path接口、DirectoryStream、Files、WatchService(重要接口更新)
fork/join framework
1、switch中添加对String类型的支持
public String generate(String name, String gender) { String title = ""; switch (gender) { case "男": title = name + " 先生"; break; case "女": title = name + " 女士"; break; default: title = name; } return title; }
编译器在编译时先做处理:
①case仅仅有一种情况。直接转成if。
②假设仅仅有一个case和default,则直接转换为if…else…。
③有多个case。先将String转换为hashCode,然后相应的进行处理,JavaCode在底层兼容Java7曾经版本号。
2、数字字面量的改进
Java7前支持十进制(123)、八进制(0123)、十六进制(0X12AB)
Java7添加二进制表示(0B11110001、0b11110001)
数字中可加入分隔符
Java7中支持在数字量中间添加’_'作为分隔符。更直观,如(12_123_456)。下划线仅仅能在数字中间。编译时编译器自己主动删除数字中的下划线。
int one_million = 1_000_000;
3、异常处理(捕获多个异常) try-with-resources
catch子句能够同一时候捕获多个异常
public void testSequence() { try { Integer.parseInt("Hello"); } catch (NumberFormatException | RuntimeException e) { //使用'|'切割,多个类型,一个对象e } }
try-with-resources语句
Java7之前须要在finally中关闭socket、文件、数据库连接等资源;
Java7中在try语句中申请资源,实现资源的自己主动释放(资源类必须实现java.lang.AutoCloseable接口,一般的文件、数据库连接等均已实现该接口,close方法将被自己主动调用)。
public void read(String filename) throws IOException { try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { StringBuilder builder = new StringBuilder(); String line = null; while((line=reader.readLine())!=null){ builder.append(line); builder.append(String.format("%n")); } return builder.toString(); } }
4、增强泛型推断
之前
Map<String, List<String>> map = new HashMap<String, List<String>>();
Java7之后可以简单的这么写
Map<String, List<String>> anagrams = new HashMap<>();
5、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); buffer.getInt(0); //值为16777216 } 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); int value = buffer.getInt(); //值为2 } /** * @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(); char result = readBuffer.getChar(); //值为'B' } /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { FileChannelUsage fcu = new FileChannelUsage(); fcu.openAndWrite(); fcu.readWriteAbsolute(); } }