project coin是Java一些语法改进的一个项目。
在Java1.7之前,switch语句只能是byte、char、short和int以及包装类和枚举常量,比如:
enum Days { MONDAY("1"), Tuesday("2"); private String value; private Days(String value) { this.value = value; }
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<>(); |