Java7语法新特性:
前言,这是大部分的特性,但还有一些没有写进去,比如多核 并行计算的支持加强 fork join 框架;这方面并没有真正写过和了解。也就不写进来了。
public String generate(String name, String gender) { String title = ""; switch (gender) { case "男": title = name + " 先生"; break; case "女": title = name + " 女士"; break; default: title = name; } return title;
public void read(String filename) throws BaseException { FileInputStream input = null; IOException readException = null; try { input = new FileInputStream(filename); } catch (IOException ex) { readException = ex; //保存原始异常 } finally { if (input != null) { try { input.close(); } catch (IOException ex) { if (readException == null) { readException = ex; } } } if (readException != null) { throw new BaseException(readException); } } }
public void read(String filename) throws IOException { FileInputStream input = null; IOException readException = null; try { input = new FileInputStream(filename); } catch (IOException ex) { readException = ex; } finally { if (input != null) { try { input.close(); } catch (IOException ex) { if (readException != null) { //此处的区别 readException.addSuppressed(ex); } else { readException = ex; } } } if (readException != null) { throw readException; } } }
public void testSequence() { try { Integer.parseInt("Hello"); } catch (NumberFormatException | RuntimeException e) { //使用'|'分割,多个类型,一个对象e } }
public void read(String filename) throws IOException { try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { StringBuilder builder = new StringBuilder(); String line = null; while((line=reader.readLine())!=null){ builder.append(line); builder.append(String.format("%n")); } return builder.toString(); } }
public void copyFile(String fromPath, String toPath) throws IOException { try ( InputStream input = new FileInputStream(fromPath); OutputStream output = new FileOutputStream(toPath) ) { byte[] buffer = new byte[8192]; int len = -1; while( (len=input.read(buffer))!=-1 ) { output.write(buffer, 0, len); } } }
public int sum(int... args) { int result = 0; for (int value : args) { result += value; } return result; }
对collections的支持
Java代码
Listlist = new ArrayList (); list.add("item"); String item = list.get(0); Set set = new HashSet (); set.add("item"); Map map = new HashMap (); map.put("key", 1); int value = map.get("key");
现在你还可以:
Listlist = ["item"]; String item = list[0]; Set set = {"item"}; Map map = {"key" : 1}; int value = map["key"];
2.自动资源管理
原来:
BufferedReader br = new BufferedReader(new FileReader(path)); try { return br.readLine(); } finally { br.close(); }
现在:
try (BufferedReader br = new BufferedReader(new FileReader(path)) { return br.readLine(); } //You can declare more than one resource to close: try ( InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest)) { // code }
3.对通用实例创建(diamond)的type引用进行了改进
原来:
Map> anagrams = new HashMap >();
现在:
Map> anagrams = new HashMap<>();
4.数值可加下划线
int one_million = 1_000_000;
6.二进制文字
int binary = 0b1001_1001;
异常catch可以一次处理完而不像以前一层层的surround;
public void newMultiCatch() { try { methodThatThrowsThreeExceptions(); } catch (ExceptionOne | ExceptionTwo | ExceptionThree e) { // log and deal with all Exceptions } }
public void newTry() { try (FileOutputStream fos = new FileOutputStream("movies.txt"); DataOutputStream dos = new DataOutputStream(fos)) { dos.writeUTF("Java 7 Block Buster"); } catch (IOException e) { // log the exception } }