1. 新的二进制数字的表达方式
如下面的二进制数常量
byte b = (byte)0b00010011;
short s = (byte)0b001000000000000;
http://download.oracle.com/javase/7/docs/technotes/guides/language/binary-literals.html
2. 使用下划线对数字进行分隔
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;
http://download.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html
3. Switch语句支持字符串变量
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;
}
http://download.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html
4.泛型实例创建时的类型推断
//Java 7之前
Map<String, List<String》 myMap = new HashMap<String, List<String》();
//Java 7
Map<String, List<String》 myMap = new HashMap<>();
<>符号是必须的。
5. 改进了使用可变参数时的编译器的错误和警告信息。
http://download.oracle.com/javase/7/docs/technotes/guides/language/non-reifiable-varargs.html
6. try-with-resources语句
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);
}
}
http://download.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html
7. 捕获多个异常,并重新抛出改进类型的异常。
catch (IOException|SQLException ex) {
logger.log(ex);
throw ex;
}