project coin是Java一些语法改进的一个项目。 java
在Java1.7以前,switch语句只能是byte、char、short和int以及包装类和枚举常量,好比: python
enum Days { 性能 MONDAY("1"), this Tuesday("2"); url private String value; spa private Days(String value) { 日志 this.value = value; orm } 对象
public String getValue() { 继承 return value; } } ... Days day = Days.MONDAY; switch(day) { case MONDAY: System.out.println("1"); break; case Tuesday: System.out.println("2"); break; default: System.out.println("3"); break;
} |
在Java7中,扩展了容许String类型,好比:
public static void printDay(String dayOfWeek) { switch (dayOfWeek) { case "Sunday": System.out.println("星期天,休息休息"); break; case "Monday": System.out.println("周一,要上班了,哎..."); break; case "Tuesday": System.out.println("次日,好无聊"); break; case "Wednesday": System.out.println("第三天了,怎么还不到周五"); break; case "Thursday": System.out.println("要崩溃了"); break; case "Friday": System.out.println("赶快下班吧,受不了了"); break; case "Saturday": System.out.println("这么晚了,又浪费了一天,那就再睡会儿吧"); break; default: System.out.println("这是星期几,哦这是火星了"); break; } } |
在Java7以前,定义一个二进制数字,须要使用以下:
int x = Integer.parseInt("1100110", 2);
这样不但冗长,并且性能也很差,Java7引入新的语法,定义二进制可使用以下方式:
x = 0b1011;
在定义很长的数字时,容易形成错误,Java7使用下划线做为分隔符来分割长数字以下:
long anotherLong = 2_147_483_648L;
int bitPattern = 0b0001_1100__0011_0111__0010_1011__1010_0011;
Java7对异常处理增长两项特性:multicatch和final rethrow。好比处理配置文件须要处理多项checked exception,好比:
public Configuration getConfig(String fileName) { Configuration cfg = null; try { String fileText = getFile(fileName); cfg = verifyConfig(parseConfig(fileText)); } catch (FileNotFoundException fnfx) { System.err.println("Config file '" + fileName + "' is missing"); } catch (IOException e) { System.err.println("Error while processing file '" + fileName + "'"); } catch (ConfigurationException e) { System.err.println("Config file '" + fileName + "' is not consistent"); } catch (ParseException e) { System.err.println("Config file '" + fileName + "' is malformed"); } return cfg; } |
有时咱们须要对FileNotFoundException和ParseException同一进行处理,Java7可使用以下方式:
public Configuration getConfig(String fileName) { Configuration cfg = null; try { String fileText = getFile(fileName); cfg = verifyConfig(parseConfig(fileText)); } catch (FileNotFoundException | ParseException | ConfigurationException e) { System.err.println("Config file '" + fileName + "' is missing or malformed"); } catch (IOException e) { System.err.println("Error while processing file '" + fileName + "'"); } return cfg; } |
有时咱们在捕获异常时,进行相应错误日志记录后会从新抛出,好比:
try { doSomethingWhichMightThrowIOException(); doSomethingElseWhichMightThrowSQLException(); } catch (Exception e) { ... throw e; } |
从新抛出的异常因为类型为Exception,由于可能会隐藏实际的异常类型,好比实际抛出的是IOException。Java7可使用在Exception前加final来告知实际抛出的异常:
try { doSomethingWhichMightThrowIOException(); doSomethingElseWhichMightThrowSQLException(); } catch (final Exception e) { ... throw e; } |
在处理IO的代码中,咱们会使用大量的try...catch()...finally...语法,其中会在finally进行IO的close操做,写过python的都知道,这种操做可使用try-with-resources操做,幸运的是Java7也有了此特性,好比以前的语法:
private void test(URL url, File file) { InputStream is = null; try { is = url.openStream(); OutputStream out = new FileOutputStream(file); try { byte[] buf = new byte[4096]; int len; while ((len = is.read(buf)) >= 0) out.write(buf, 0, len); } catch (IOException iox) { } finally { try { out.close(); } catch (IOException closeOutx) { } } } catch (FileNotFoundException fnfx) { } catch (IOException openx) { } finally { try { if (is != null) is.close(); } catch (IOException closeInx) { } } } |
而使用try-with-resources语法,则能够简化为:
try (OutputStream out = new FileOutputStream(file); InputStream is = url.openStream()) { byte[] buf = new byte[4096]; int len; while ((len = is.read(buf)) > 0) { out.write(buf, 0, len); } } |
可是使用try-with-resources的时候仍是由可能形成资源没有关闭,好比在try()中有错误时,好比:
try ( ObjectInputStream in = new ObjectInputStream(new FileInputStream("someFile.bin")) ) { ... } |
好比文件存在,但却不是写入的对象序列,所以会形成不正常打开,此时ObjectInputStream不能正确初始化,且不会关闭,所以正确的方式是分开资源变量:
try ( FileInputStream fin = new FileInputStream("someFile.bin"); ObjectInputStream in = new ObjectInputStream(fin) ) { ... } |
TWR特性是使用了java7的新的接口AutoCloseable,可使用try-with-resources语法的资源必须实现该接口。而Closeable继承了AutoCloseable,所以咱们经常使用的资源类均可以使用。
diamond syntax是用来简化泛型的,好比:
Map<Integer, Map<String, String>> usersLists = new HashMap<Integer, Map<String, String>>(); |
能够简化为:
Map<Integer, Map<String, String>> usersLists = new HashMap<>(); |