7月29日, Java SE 7正式发布! 经过与世界范围内的Java社区, Java平台历时将近五年的协作, 标准版本已经可以 下载 了.
新特性
- 改进动态语言支持
- 改进的IO, 加强对NIO的支持 详见 Enhancements in Java I/O
- 支持非Java语言: Java SE 7 引入一个新的 JVM 指令用于简化实现动态类型编程语言
- Garbage-First Collector 新的针对服务器的垃圾收集器用于替换CMS. What is the Garbage-First Collector?
- 基于ForkJoinPool类的fork/join框架. 适合于多处理器环境下用来进行高效的运行大量任务. 详见 Concurrency Utilities Enhancements in Java SE 7
- 增强国际化支持. Internationalization Enhancements in Java SE 7
语言特性
二进制数字表达方式
在java7中, 整形(byte, short, int, and long)可以用二进制表示, 加上前缀 0b 或者 0B 就可以了.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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中依然可以使用 catch
和 finally
, catch
和
finally
会在资源关闭之后运行.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; }
数字表达中使用下划线
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//正确 | |
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};
是不是有些动态语言的味道
闭包也没有, 可惜…