Java7 新特性

来源:互联网 发布:淘宝买iphone7靠谱吗 编辑:程序博客网 时间:2024/05/21 06:15

摘要

  • switch中使用string
  • 泛型实例化类型自动推断
  • 二进制数字表达方式
  • 使用下划线对数字进行分隔表达,例如 1_322_222
  • 同时捕获多个异常处理
  • 自动资源管理
  • -

1、switch中使用string:

public void test(String s) {        switch (s) {            case "aaa":                System.out.println("aaa");                break;            case "bbb":                System.out.println("bbb");                break;            case "ccc":                System.out.println("ccc");                break;            default:                break;        }    }

2、泛型实例化类型自动推断:

List<String> tempList = new ArrayList<>(); 

3、二进制数字表达方式

// An 8-bit 'byte' value:byte aByte = (byte)0b00100001;// A 16-bit 'short' value:short aShort = (short)0b1010000101000101;// Some 32-bit 'int' values:int anInt1 = 0b10100001010001011010000101000101;int anInt2 = 0b101;int anInt3 = 0B101; // The B can be upper or lower case.// A 64-bit 'long' value. Note the "L" suffix:long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;int[] phases = {0x31, 0x62, 0xC4, 0x89, 0x13, 0x26, 0x4C, 0x98};

4、使用下划线对数字进行分隔表达,例如 1_322_222

        float pi1 = 3_.1415F;      // Invalid; 不能在小数点附近放置下划线        float pi2 = 3._1415F;      // Invalid; 不能在小数点附近放置下划线        long socialSecurityNumber1 = 999_99_9999_L;         // Invalid; 不能将下划线放在L前面        int x1 = _52;              // Invalid; This is an identifier, not a numeric literal        int x2 = 5_2;              // OK (decimal literal)        int x3 = 52_;              // Invalid; 不能将下划线放在最后        int x4 = 5_______2;        // OK (decimal literal)        int x5 = 0_x52;            // Invalid; 不能将下划线放在0x之间        int x6 = 0x_52;            // Invalid; 不能将下划线放在数字的开头        int x7 = 0x5_2;            // OK (hexadecimal literal)        int x8 = 0x52_;            // Invalid; 不能将下划线放在数字的结尾        int x9 = 0_52;             // OK (octal literal)        int x10 = 05_2;            // OK (octal literal)        int x11 = 052_;            // 不能将下划线放在数字的结尾

5、同时捕获多个异常处理:

        try {            //code...        }catch (IOException|SQLException ex) {            throw ex;        }

6、自动资源管理
Java中某些资源是需要手动关闭的,如InputStream,Writes,Sockets,Sql classes等。这个新的语言特性允许try语句本身申请更多的资源,这些资源作用于try代码块,并自动关闭。

         try (BufferedReader br = new BufferedReader(new FileReader(path)) {              return br.readLine();         } 

7、Java I/O:

java.nio.file 包以及相关的包 java.nio.file.attribute 提供对文件 I/O 以及访问文件系统的全面支持,请看 File I/O (featuring NIO.2).

  • 目录 /sample/nio/chatserver/ 包含使用 java.nio.file 包的演示程序
  • 目录 /demo/nio/zipfs/ 包含 NIO.2 NFS 文件系统的演示程序

8、Java 虚拟机

  • 支持非 Java 语言: Java SE 7 引入一个新的 JVM 指令用于简化实现动态类型编程语言
  • Garbage-First Collector 是一个服务器端的垃圾收集器用于替换 Concurrent Mark-Sweep Collector (CMS).
  • 提升了 Java HotSpot 虚拟机的性能

9、并发:

  • fork/join 框架,基于 ForkJoinPool 类,是 Executor 接口的实现,设计它用来进行高效的运行大量任务;
  • 使用 work-stealing 技术用来保证大量的 worker 线程工作,特别适合多处理器环境,详情请看 Fork/Join
  • 目录/sample/forkjoin/ 包含了 fork/join 框架的演示程序
  • ThreadLocalRandom 类class 消除了使用伪随机码线程的竞争,请看 Concurrent Random Numbers.
  • Phaser 类是一个新的同步的屏障,与 CyclicBarrier 类似.
0 0
原创粉丝点击