1.二进制字面量
JDK7开始,终于可以用二进制来表示整数(byte,short,int和long)。使用二进制字面量的好处是,可以是代码更容易被理解。语法非常简单,只要在二进制数值前面加 0b或者0B
如:
byte nByte = (byte) 0b1;
short nShort = (short) 0B10;
int nInt = 0b11;
long nLong = 0b100L;
System.out.println(nByte);//1
System.out.println(nShort);//2
System.out.println(nInt);//3
System.out.println(nLong);//4
2.数字如果觉得太大,可以在数字间用_来更好可视化
如:
int a = 100_0000;
System.out.println(a);//1000000
3.switch 语句可以用字符串了
这个功能千呼万唤,终于出来了
private static void switchString(String str){
switch(str){
case “one”:
System.err.println(“1”);
break;
case “two”:
System.out.println(“2”);
break;
default :
System.out.println(“err”);
}
}
4.泛型实例的创建可以通过类型推断来简化
以后你创建一个泛型实例,不需要再详细说明类型,只需用<>,编译器会自动帮你匹配
7.Catch多个Exception,rethrow exception 改进了类型检测
很多时候,我们捕获了多个异常,却做了相同的事情,比如记日志,包装成新的异常,然后rethrow。这时,代码就不那么优雅了,例如
catch (IOException ex) {
logger.log(ex);
throw ex;
catch (SQLException ex) {
logger.log(ex);
throw ex;
}
Jdk7允许捕获多个异常
catch (IOException|SQLException ex) {
logger.log(ex);
throw ex;
}
注意,catch后面的异常参数是final的,不能重新再复制