/**
* 从java7开始catch异常可以catch多个
*/
public class ExceptionCatch {
public static void main(String[] args) {
//java7之前捕捉多个异常需要多个catch
try {
throwAll();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
//java7开始可以catch多个异常
try {
throwAll();
} catch (IOException | SQLException e) {
e.printStackTrace();
}
}
public static void throwAll() throws IOException, SQLException {
ioThrow();
sqlThrow();
}
public static void ioThrow() throws IOException {
throw new IOException();
}
public static void sqlThrow() throws SQLException {
throw new SQLException();
}
}
泛型实例化类型自动推断
/**
* 泛型实例化类型自动推断
*/
public class Genericity {
public static void main(String[] args) {
//java7之前
List<String> listBefore7=new ArrayList<String>();
//java7之后
List<String> listAfter7=new ArrayList<>();
}
}
增加二进制表示
/**
* 在java开始新增了二进制表示
*/
public class Binary {
public static void main(String[] args) {
//二进制
int a = 0b0001_1001;
//8进制,不能少了最前的那个0
int b=0144;
//十进制
int c=123;
//16进制
int d=0x100F;
System.out.println("二进制:"+a);
System.out.println("8进制:"+b);
System.out.println("十进制:"+c);
System.out.println("16进制:"+d);
}
}
数字中可添加分隔符
/**
* Java7中支持在数字中间增加'_'作为分隔符,分隔长int以及long(也支持double,float),显示更直观,如(12_123_456)。
* 下划线只能在数字中间,编译时编译器自动删除数字中的下划线。
*/
public class NumberSplit {
public static void main(String[] args) {
int intOne = 1_000_000;
long longOne = 1_000_000;
double doubleOne = 1_000_000;
float floatOne = 1_000_000;
System.out.println("intOne:"+intOne);
System.out.println("longOne:"+longOne);
System.out.println("doubleOne:"+doubleOne);
System.out.println("floatOne:"+floatOne);
}
}