java7特性

介绍了java7的新特性

  • 增加了二进制字面量
  • 数字字面量支持用下划线分割
  • 可以自动检测并匹配泛型
  • try-with-resource
  • catch可以处理多重exception
  • 可以在方法声明处精确的throw具体的exception

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	public class NewFeatureTest {
public static void main(String[] args) throws IOException, SQLException {
int a=0b1111_0000_1111_0000; //0bxxxxxx, express binary literals
long m=123_4444_123L; //xxx_xxx_xx, number literals can be splitted by slash
System.out.println(a);
System.out.println(m);

List<String> str = new ArrayList<>(); //Auto-detect generic type

try (BufferedReader br = new BufferedReader(new FileReader("dssf.jpg"))) { //try-with-resource, resource will be assured to close before catch or finally
System.out.println(br.readLine());
}catch (NullPointerException | FileNotFoundException e){ //catch can get multiple exceptions
e.printStackTrace();
}
}
}