jdk7和8的一些新特性介绍 (一)

2014-11-24 10:21:36 · 作者: · 浏览: 5
本文是我学习了解了jdk7和jdk8的一些新特性的一些资料,有兴趣的大家可以浏览下下面的内容。
官方文档:http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html

在jdk7的新特性方面主要有下面几方面的增强:

1.jdk7语法上

   1.1二进制变量的表示,支持将整数类型用二进制来表示,用0b开头。

   // 所有整数 int, short,long,byte都可以用二进制表示
	// An 8-bit 'byte' value:
	byte aByte = (byte) 0b00100001;

	// A 16-bit 'short' value:
	short aShort = (short) 0b1010000101000101;

	// Some 32-bit 'int' values:
	intanInt1 = 0b10100001010001011010000101000101;
	intanInt2 = 0b101;
	intanInt3 = 0B101; // The B can be upper or lower case.

	// A 64-bit 'long' value. Note the "L" suffix:
	long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;

	// 二进制在数组等的使用
	final int[] phases = { 0b00110001, 0b01100010, 0b11000100, 0b10001001,
	0b00010011, 0b00100110, 0b01001100, 0b10011000 };

1.2  Switch语句支持string类型 

	   public static 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;
	} 

1.3 Try-with-resource语句 
  
  注意:实现java.lang.AutoCloseable接口的资源都可以放到try中,跟final里面的关闭资源类似; 按照声明逆序关闭资源 ;Try块抛出的异常通过Throwable.getSuppressed获取 

	try
(java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName); java.io.BufferedWriter writer = java.nio.file.Files .newBufferedWriter(outputFilePath, charset)) { // Enumerate each entry for (java.util.Enumeration entries = zf.entries(); entries .hasMoreElements();) { // Get the entry name and write it to the output file String newLine = System.getProperty("line.separator"); String zipEntryName = ((java.util.zip.ZipEntry) entries .nextElement()).getName() + newLine; writer.write(zipEntryName, 0, zipEntryName.length()); } } 1.4 Catch多个异常 说明:Catch异常类型为final; 生成Bytecode 会比多个catch小; Rethrow时保持异常类型 public static void main(String[] args) throws Exception { try { testthrows(); } catch (IOException | SQLException ex) { throw ex; } } public static void testthrows() throws IOException, SQLException { } 1.5 数字类型的下划线表示 更友好的表示方式,不过要注意下划线添加的一些标准,可以参考下面的示例 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; // Invalid; cannot put underscores prior to an L suffix //int x1 = _52; // This is an identifier, not a numeric literal int x2 = 5_2; // OK (decimal literal) //int x3 = 52_; // Invalid; cannot put underscores at the end of a literal int x4 = 5_______2