代码改变世界

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, and long) can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B 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 a switch 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 a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements the new java.lang.AutoCloseable interface or the java.io.Closeable interface can be used as a resource. The classes java.io.InputStream, OutputStream, Reader, Writer, java.sql.Connection, Statement, and ResultSet have been retrofitted to implement the AutoCloseable interface and can all be used as resources in a try-with-resources statement.
  • try-with-resources语句, 任何使用了java.lang.AutoCloseablejava.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 the throws 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的.