Java 8早都出来了,现在来了解一下Java 7语言上的几个新特性。 :) switch语句支持String、数字常量的新形式、改进的异常处理、TWR语句、钻石语法和变参警告位置的修改。
另一个新语法对需要重新抛出异常时很有用:
try { doSomethingWhichMightThrowIOException(); doSomethingElseWhichMightThrowSQLException(); } catch (final Exception e) { ... //不再是抛出笼统的Exception,而是抛出实际的异常。 //final不是必须的,但留着提个醒有好处。 throw e; }这个很有用,特别是io操作时,可以抛掉大串丑陋的代码了。
try ( OutputStream out = new FileOutputStream(file); InputStream is = url.openStream() ) { byte[] buf = new byte[4096]; int len; while (len = is.read(buf)) > 0) out.write(buf, 0, len); }上面的代码将资源放在try的圆括号内,当处理完后会自动关闭!但一定要注意不要嵌套创建,否则可能无法正确关闭。一定要声明变量。例如下面的代码就应该修改:
try (ObjectInputStream in = new ObjectInputStream( new FileInputStream("someFile.bin"))) { ... } //要改为: try ( FileInputStream fin = new FileInputStream("someFile.bin"); ObjectInputStream in = new ObjectInputStream(fin)) { ... }TWR特性依赖于try从句中的资源类实现新接口AutoCloseable。Java 7平台的大多数资源都已经修改过了。
在Java 7之前,如果泛型和变参结合起来会怎么样?
public static <T> Collection<T> doSomething(T... entries) { ... }Java处理变参实际上是把它放到一个编译器自动创建的数组中。但我们知道泛型的实现其实是通过擦拭法实现的。所以Java数组不支持泛型:
HashMap<String, String>[] a = new HashMap<String, String>[3]; //编译错误 HashMap<String, String>[] a = new HashMap[3]; //编译可通过,但会有警告: //Type safety: The expression of type HashMap[] needs unchecked conversion to conform to HashMap<String,String>[]因此,当泛型遇到变参时,编译器只好给你个警告。但这个问题更应该由API的设计者去关注,而不是API使用者。所以Java 7把警告信息挪到了定义API的地方。
转载:http://blog.ubone.com/blog/2014/11/18/java-7de-6ge-xin-te-xing/
2014-11-18
