Java7的那些新特性

BurReda 8年前

来自: http://blog.csdn.net//chenleixing/article/details/47802653


本文介绍的java 7新特性更多的感觉像是语法糖。毕竟java本身已经比较完善了,不完善的很多比较难实现或者是依赖于某些底层(例如操作系统)的功能。不过java7也实现了类似aio的强大功能。但本文并未有此介绍。主要是 1.switch可以接受string类型而不像以前仅仅是int;2.异常catch可以一次处理完而不像以前一层层的surround;3.泛型类实例化也不用繁琐的将泛型声明再写一遍;4.文件读写 会自动关闭流而不像以前那样需要在finally中显式close;5.数值可以使用下划线分隔;6.文件读写功能增强,有更简单的api调用;7.文件改变的事件通知功能;8.多核 并行计算的支持加强 fork join 框架;9.还有一些动态特性的加入。

具体看代码:

1.switch可以接受string类型而不像以前仅仅是int;

[html]   view plain copy
  1. public void processTrade(Trade t) {  
  2.   
  3.             String status = t.getStatus();  
  4.   
  5.    
  6.   
  7.             switch (status) {  
  8.   
  9.             case NEW:  
  10.   
  11.                   newTrade(t);  
  12.   
  13.                   break;  
  14.   
  15.             case EXECUTE:  
  16.   
  17.                   executeTrade(t);  
  18.   
  19.                   break;  
  20.   
  21.             case PENDING:  
  22.   
  23.                   pendingTrade(t);  
  24.   
  25.                   break;  
  26.   
  27.    
  28.   
  29.             default:  
  30.   
  31.                   break;  
  32.   
  33.             }  
  34.   
  35.       }  
  36.         
2.异常catch可以一次处理完而不像以前一层层的surround;

[html]   view plain copy
  1. public void newMultiCatch() {  
  2.   
  3.            try {  
  4.   
  5.                  methodThatThrowsThreeExceptions();  
  6.   
  7.            } catch (ExceptionOne | ExceptionTwo | ExceptionThree e) {  
  8.   
  9.                  // log and deal with all Exceptions  
  10.   
  11.            }  
  12.   
  13.      }  


3.泛型类实例化也不用繁琐的将泛型声明再写一遍;

[html]   view plain copy
  1. Map<String, List<Trade>> trades = new TreeMap <> ();  

4.文件读写 会自动关闭流而不像以前那样需要在finally中显式close;

[html]   view plain copy
  1. public void newTry() {  
  2.   
  3.   
  4.   
  5.           try (FileOutputStream fos = new FileOutputStream("movies.txt");  
  6.   
  7.                       DataOutputStream dos = new DataOutputStream(fos)) {  
  8.   
  9.                 dos.writeUTF("Java 7 Block Buster");  
  10.   
  11.           } catch (IOException e) {  
  12.   
  13.                 // log the exception  
  14.   
  15.           }  
  16.   
  17.     }  


5.数值可以使用下划线分隔;

[html]   view plain copy
  1. int million  =  1_000_000  

6.文件读写功能增强,有更简单的api调用;

          [html]      view plain     copy                   public void pathInfo() {                        Path path = Paths.get("c:\\Temp\\temp");            System.out.println("Number of Nodes:" + path.getNameCount());                        System.out.println("File Name:" + path.getFileName());                        System.out.println("File Root:" + path.getRoot());                        System.out.println("File Parent:" + path.getParent());                                    //这样写不会抛异常                  Files.deleteIfExists(path);       }     

7.文件改变的事件通知功能;

[html]   view plain copy
  1. /**  
  2.   
  3.  * This initiates the police  
  4.   
  5.  */  
  6.   
  7. private void init() {  
  8.   
  9.       path = Paths.get("C:\\Temp\\temp\\");  
  10.   
  11.       try {  
  12.   
  13.             watchService = FileSystems.getDefault().newWatchService();  
  14.   
  15.             path.register(watchService, ENTRY_CREATE, ENTRY_DELETE,  
  16.   
  17.                         ENTRY_MODIFY);  
  18.   
  19.       } catch (IOException e) {  
  20.   
  21.             System.out.println("IOException"+ e.getMessage());  
  22.   
  23.       }  
  24.   
  25. }  
  26.   
  27. /**  
  28.   
  29.  * The police will start making rounds  
  30.   
  31.  */  
  32.   
  33. private void doRounds() {  
  34.   
  35.       WatchKey key = null;  
  36.   
  37.       while(true) {  
  38.   
  39.             try {  
  40.   
  41.                   key = watchService.take();  
  42.   
  43.                   for (WatchEvent<?> event : key.pollEvents()) {  
  44.   
  45.                         Kind<?> kind = event.kind();  
  46.   
  47. System.out.println("Event on " + event.context().toString() + " is " + kind);  
  48.   
  49.                   }  
  50.   
  51.             } catch (InterruptedException e) {  
  52.   
  53. System.out.println("InterruptedException: "+e.getMessage());  
  54.   
  55.             }  
  56.   
  57.             boolean reset = key.reset();  
  58.   
  59.             if(!reset)  
  60.   
  61.                   break;  
  62.   
  63.       }  
  64.   
  65. }  


8.多核 并行计算的支持加强 fork join 框架;

[html]   view plain copy
  1. ForkJoinPool pool = new ForkJoinPool(numberOfProcessors);  
  2.   
  3. public class MyBigProblemTask extends RecursiveAction {  
  4.   
  5.    
  6.   
  7.     @Override  
  8.   
  9.     protected void compute() {  
  10.   
  11.         . . . // your problem invocation goes here  
  12.   
  13.     }  
  14.   
  15. }  
  16.   
  17. pool.invoke(task);  


9.还有一些动态特性的加入。

java.lang.invoke 包的引入。 MethodHandle, CallSite 还有一些其他类供使用。


具体参见原文 http://radar.oreilly.com/2011/09/java7-features.html

更多内容,大家可参考:

Java 7 的新特性一览表