CODE4FUN
甲骨文发布Java SE 7正式版

7月29日, Java SE 7正式发布! 经过与世界范围内的Java社区, Java平台历时将近五年的协作, 标准版本已经可以 下载 了.

新特性

语言特性

二进制数字表达方式

在java7中, 整形(byte, short, int, and long)可以用二进制表示, 加上前缀 0b 或者 0B 就可以了.

// An 8-bit 'byte' value:
byte aByte = (byte)0b00100001;
// A 16-bit 'short' value:
short aShort = (short)0b1010000101000101;
// Some 32-bit 'int' values:
int anInt1 = 0b10100001010001011010000101000101;
int anInt2 = 0b101;
int anInt3 = 0B101; // The B can be upper or lower case.
// A 64-bit 'long' value. Note the "L" suffix:
long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;

switch支持字符串变量

这可是期盼多年的特性. 表达式比较使用 String.equals 方法

public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
String typeOfDay;
switch (dayOfWeekArg) {
case "Monday":
typeOfDay = "Start of work week";
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
typeOfDay = "Midweek";
break;
case "Friday":
typeOfDay = "End of work week";
break;
case "Saturday":
case "Sunday":
typeOfDay = "Weekend";
break;
default:
throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
}
return typeOfDay;
}

try-with-resources 语句

在资源使用完后自动关闭. 资源是指实现了 java.lang.AutoCloseable 或者 Closeable 接口的类.\ 注意在try-with-resources中依然可以使用 catchfinally, catchfinally 会在资源关闭之后运行.

public static void viewTable(Connection con) throws SQLException {
String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES";
try (Statement stmt = con.createStatement()) {
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + ", " + supplierID + ", " + price +
", " + sales + ", " + total);
}
} catch (SQLException e) {
JDBCTutorialUtilities.printSQLException(e);
}
}

同时捕获多个异常

catch (IOException|SQLException ex) { logger.log(ex); throw ex; }

数字表达中使用下划线

//正确
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
//错误
float pi1 = 3_.1415F; // Invalid; cannot put underscores adjacent to a decimal point
float pi2 = 3._1415F; // Invalid; cannot put underscores adjacent to a decimal point
long socialSecurityNumber1 = 999_99_9999_L; // Invalid; cannot put underscores prior to an L suffix
int x3 = 52_; // Invalid; cannot put underscores at the end of a literal
int x4 = 5_______2; // OK (decimal literal)
int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix
int x6 = 0x_52; // Invalid; cannot put underscores at the beginning of a number
int x7 = 0x5_2; // OK (hexadecimal literal)
int x8 = 0x52_; // Invalid; cannot put underscores at the end of a number
int x11 = 052_; // Invalid; cannot put underscores at the end of a number

泛型在创建实例时的类型引用

之前的代码 Map<String, List<String>> myMap = new HashMap<String, List<String>>(); Java7的代码 Map<String, List<String>> myMap = new HashMap<>(); 编译器可以根据声明自动推断范型的类型.

Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods

详细: 链接


可惜那个很酷的集合声明语法没有实现 List<String> people = {"Frank", "Mary", "Satan"}; Map<String, Integer> map = {"key" : 1}; 是不是有些动态语言的味道

闭包也没有, 可惜…