Java SE 7 新特性[没有对集合的增强支持]
2011-09-26 11:21 会被淹死的鱼 阅读(837) 评论(0) 编辑 收藏 举报Enhancements in Java SE 7
- Binary Literals - In Java SE 7, the integral types (
byte
,short
,int
, andlong
) can also be expressed using the binary number system. To specify a binary literal, add the prefix0b
or0B
to the number. - 二进制数值的支持, 0b或0B表示二进制.
- Underscores in Numeric Literals - Any number of underscore characters (
_
) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code. - 数字类型可以使用下划线来分块, 比如1_000.
- Strings in switch Statements - You can use the
String
class in the expression of aswitch
statement. - 增强的switch, 可以在switch中使用string类型.
- Type Inference for Generic Instance Creation - You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (
<>
) as long as the compiler can infer the type arguments from the context. This pair of angle brackets is informally called the diamond. - 更加智能的泛型, 例如ArrayList<String> list = new ArrayList<>();, 在构造函数中, 不需要再次说明类型, 只需加<>(叫做diamond), 编译器可以从上下文推断出类型参数.
- Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods - The Java SE 7 complier generates a warning at the declaration site of a varargs method or constructor with a non-reifiable varargs formal parameter. Java SE 7 introduces the compiler option
-Xlint:varargs
and the annotations@SafeVarargs
and@SuppressWarnings({"unchecked", "varargs"})
to supress these warnings. - 在使用可变参量, 可以使用
编译器选项-Xlint:varargs
, 或注解@SafeVarargs
和@SuppressWarnings({"unchecked", "varargs"})
, 来消除警告. - The try-with-resources Statement - The
try
-with-resources statement is atry
statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. Thetry
-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements the newjava.lang.AutoCloseable
interface or thejava.io.Closeable
interface can be used as a resource. The classesjava.io.InputStream
,OutputStream
,Reader
,Writer
,java.sql.Connection
,Statement
, andResultSet
have been retrofitted to implement theAutoCloseable
interface and can all be used as resources in atry
-with-resources statement. - try-with-resources语句, 任何使用了
java.lang.AutoCloseable
和java.io.Closeable
接口的对象, 可以被当作resource, 在try-with-resources语句保证这些resource在语句结束时关闭. - Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking - A single
catch
block can handle more than one type of exception. In addition, the compiler performs more precise analysis of rethrown exceptions than earlier releases of Java SE. This enables you to specify more specific exception types in thethrows
clause of a method declaration. - 捕获多个异常类型和增强类型检查的重抛异常.一个catch可以处理多个类型的异常.此外, 编译器拥有了比以前更加准确的重抛异常分析, 这样就可以在方法声明的throws语句中指定更加具体的异常类型.
很多人说jdk7增加了对Java集合(Collections)的增强支持, 但是我自己用jdk7的时候, 有语法错误, 编译不通过. 我查看官网没有发现有这个新特性.
http://download.oracle.com/javase/7/docs/technotes/guides/language/enhancements.html#javase7
目前eclipse 3.7.1已经增加了对jdk7的支持, Intellij IDEA是最早支持jdk 7的.
作者:icejoywoo
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
短网址: http://goo.gl/ZiZCi