Java7的那些新特性
來自: 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;
- public void processTrade(Trade t) {
- String status = t.getStatus();
- switch (status) {
- case NEW:
- newTrade(t);
- break;
- case EXECUTE:
- executeTrade(t);
- break;
- case PENDING:
- pendingTrade(t);
- break;
- default:
- break;
- }
- }
- public void newMultiCatch() {
- try {
- methodThatThrowsThreeExceptions();
- } catch (ExceptionOne | ExceptionTwo | ExceptionThree e) {
- // log and deal with all Exceptions
- }
- }
3.泛型類實例化也不用繁瑣的將泛型聲明再寫一遍;
- Map<String, List<Trade>> trades = new TreeMap <> ();
4.文件讀寫 會自動關閉流而不像以前那樣需要在finally中顯式close;
- 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
- }
- }
5.數值可以使用下劃線分隔;
- 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.文件改變的事件通知功能;
- /**
- * This initiates the police
- */
- private void init() {
- path = Paths.get("C:\\Temp\\temp\\");
- try {
- watchService = FileSystems.getDefault().newWatchService();
- path.register(watchService, ENTRY_CREATE, ENTRY_DELETE,
- ENTRY_MODIFY);
- } catch (IOException e) {
- System.out.println("IOException"+ e.getMessage());
- }
- }
- /**
- * The police will start making rounds
- */
- private void doRounds() {
- WatchKey key = null;
- while(true) {
- try {
- key = watchService.take();
- for (WatchEvent<?> event : key.pollEvents()) {
- Kind<?> kind = event.kind();
- System.out.println("Event on " + event.context().toString() + " is " + kind);
- }
- } catch (InterruptedException e) {
- System.out.println("InterruptedException: "+e.getMessage());
- }
- boolean reset = key.reset();
- if(!reset)
- break;
- }
- }
8.多核 并行計算的支持加強 fork join 框架;
- ForkJoinPool pool = new ForkJoinPool(numberOfProcessors);
- public class MyBigProblemTask extends RecursiveAction {
- @Override
- protected void compute() {
- . . . // your problem invocation goes here
- }
- }
- pool.invoke(task);
9.還有一些動態特性的加入。
java.lang.invoke
包的引入。 MethodHandle
, CallSite
還有一些其他類供使用。
具體參見原文 http://radar.oreilly.com/2011/09/java7-features.html
更多內容,大家可參考:
Java 7 的新特性一覽表
本文由用戶 BurReda 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!