日期:2014-05-16  浏览次数:20305 次

Java 7 新语言特性(JSR 334: Small Enhancements)

JDK7有不少新东东, 语言特性里有一些更新, 虽然不如 JDK1.5 的泛型/可变参数/for-each循环那么激动人心, 但在平时开发中还是有很大帮助. 个人感觉 try 语句的新特性最有用. 参考: JSR-334

写篇文章记录一下.

public class CommonTest {

	public static void main(String[] args) {

		List< String> list = ["item"];
		String item = list[0];
		Set< String > set = {"item"};
		Map< String,Integer > map = {"key" : 1};
		int value = map["key"];

		// try-with-resources statement
		try (InputStream in = System.in; OutputStream out = System.out) {

			// Binary integral literals and underscores in numeric literals
			int a = 32_242;
			int b = 0B011_101;
			int c = 023_273;
			int d = 0x23_2c_ab;

			// Improved Type Inference for Generic Instance Creation (diamond)
			List<String> aa = new ArrayList<>();

			// Strings in switch
			String str = "str";
			switch (str) {

			}

			// Multi-catch and more precise rethrow
		} catch (IllegalArgumentException | IndexOutOfBoundsException e) {
		} catch (IOException e) {
		}

	}

	// Simplified Varargs Method Invocation
	@SafeVarargs
	public static  void f(List... t) {

	}

}