Java7的那些新特性

BurReda 8年前發布 | 9K 次閱讀 Java開發

來自: 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 的新特性一覽表

 

 

 

 本文由用戶 BurReda 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!