在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java7的那些新特性

2017-2-24 13:01| 发布者: zhangjf| 查看: 1333| 评论: 0

摘要: 来自: http://blog.csdn.net//chenleixing/article/details/47802653 本文介绍的java 7新特性更多的感觉像是语法糖。毕竟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 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; } } 2.异常catch可以一次处理完而不像以前一层层的surround;

[html] view plain copy public void newMultiCatch() { try { methodThatThrowsThreeExceptions(); } catch (ExceptionOne | ExceptionTwo | ExceptionThree e) { // log and deal with all Exceptions } }

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

[html] view plain copy Map> trades = new TreeMap <> ();
4.文件读写 会自动关闭流而不像以前那样需要在finally中显式close;

[html] view plain copy 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.数值可以使用下划线分隔;

[html] view plain copy int million = 1_000_000

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

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

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

[html] view plain copy /** * 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 框架;

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

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 08:45

Copyright 2015-2025 djqfx

返回顶部