zoukankan      html  css  js  c++  java
  • Java7的那些新特性

    本文介绍的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 plaincopy
    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 plaincopy
    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 plaincopy
    1. Map<String, List<Trade>> trades = new TreeMap <> ();  

    4.文件读写 会自己主动关闭流而不像曾经那样须要在finally中显式close。

    [html] view plaincopy
    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 plaincopy
    1. int million  =  1_000_000  

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

    1. public void pathInfo() {  
    2.   
    3.             Path path = Paths.get("c:\Temp\temp");  
    4.   
    5. System.out.println("Number of Nodes:" + path.getNameCount());  
    6.   
    7.             System.out.println("File Name:" + path.getFileName());  
    8.   
    9.             System.out.println("File Root:" + path.getRoot());  
    10.   
    11.             System.out.println("File Parent:" + path.getParent());   
    12.              
    13.             //这样写不会抛异常  
    14.             Files.deleteIfExists(path);  
    15.  }  

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

    [html] view plaincopy
    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 plaincopy
    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 的新特性一览表

     

     

  • 相关阅读:
    多线程第四篇秒杀 一个经典的多线程同步问题
    用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能
    用JSP实现的商城购物车模块
    深入浅出JMS(一)——JMS简单介绍
    hdu 1700 Points on Cycle 水几何
    回调函数
    VC中获取窗体句柄的各种方法
    virus.win32.parite.H病毒的查杀方法
    [WF4.0 实战] WPF + WCF + WF 打造Hello World(基础篇)
    HDU 4946 Area of Mushroom 凸包 第八次多校
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7086463.html
Copyright © 2011-2022 走看看