实践jdk7新特性

Binary Literal

Underscore in Numeric Literals

Strings in switch Statement

Type Inference for Generic Instance Creation

try-with-resources

1 Exception Handling Changes

1.1、try-with-resources

try(Resource res = …;…){

work with res

}

Resource实现AutoCloseable接口,会自动调用close()方法

1.2、Suppress Exception

java.lang.Throwable#getSuppressed

1.3、Catching Multiple Exceptions

catch(FileNotFoundException UnKnownHostException ex)

1.4、Easier Exception Handling for Reflectvie Methods

Class.forName(className).getMethod(“main”).invoke(null, new String[] {});

需捕获多个异常

catch (ClassNotFoundException NoSuchMethodException
IllegalAccessException InvocationTargetException ex) { … }

新增ReflectiveOperationException代表全部

catch (ReflectiveOperationException ex) { … }

Support for Dynamic Languages

Java nio Package

Diamond Syntax

Automatic null Handling

Support for Dynamic Languages

2、Working with Files

2.1、Paths、Path

2.2、Reading and Writing Files

Files

Watchable、StandardWatchEventKinds

3、Implementing the equals,hashcode and compareTo method

3.1、Objects.equals、Objects.hashCode、Objects.equals

Comparing Numeric Types

x-other.x // Risk of overflow when x is large and other.x is negative

Integer.compare(x,other.x) // No risk of overflow

参考

Java Versions, Features and History

Java Programming Language Enhancements