• Java JDK 1.7 和 JDK 1.8 新特性


    引言

      本文主要介绍 Java JDK 中 1.7 和 1.8 的新特性。

    JDK 1.7 新特性

      1. switch可以接受String类型;

    public class Switch {
    	public void test(String str) {
    		switch (str) {
    		case "computer":
    			System.out.println("computer");
    			break;
    		case "book":
    			System.out.println("book");
    			break;
    		case "phone":
    			System.out.println("phone");
    			break;
    		default:
    			System.out.println("default");
    			break;
    		}
    	}
    }

      2. 可以在catch代码块中捕获多个异常类型;

    try {
    	//可能会抛出Exception1和Exception2异常的代码
    }catch(Exception1 | Exception2 3) {
    	//处理异常的代码
    }
    

      3. 对数值字面量进行了改进;

      (1)增加了二进制字面量的表示(0B001、0b111)。整数类型(例如byte、short、int、long等)也可以使用二进制数来表示。

      (2)在数字中可以添加分隔符,例如123_456,下划线只能被用在数字中间,编译的时候这些下划线会被编译器去掉。好处:避免了一些难以观察代码来发现的细微错误。

      4. 使用泛型增加了类型推断机制;

    Map<String, String> map = new HashMap<>();
    

      5. 增加了try-with-resources语句,确保每个资源都在生命周期结束后被关闭

      6. 增加了fork/join框架来增强对处理多核并行计算的支持

    JDK 1.8 新特性

      1. 增加了对lambda表达式的支持。

            //这是带参数类型的Lambda的写法
            public void testLamda1(){
            List<String> list =Arrays.asList("aaa","fsa","ser","eere");
            Collections.sort(list, (Comparator<? super String>) (String a,String b)->{
                return b.compareTo(a);
            }
            );
            for (String string : list) {
                System.out.println(string);
            }
        }    
    

      2. default关键字:default方法是所有的实现类都不需要去实现的就可以直接调用

      3. 函数式接口

      4. 方法与构造函数引用

      5. 局部变量限制

      6. Date API 更新:定义了多种常量格式

      7. 流

  • 相关阅读:
    [hosts]在hosts中屏蔽一级域名和二级域名的写法
    [oracle]查询一个表中数据的插入时间
    [Windows Doc]微软官方文档
    [PL]如果天空是黑暗的,那就摸黑生存
    [LVM]创建LVM卷
    [powershell]获取FCID&Port
    [oracle]解决ora-01034 oracle not available
    [GoogleBlog]new-approach-to-china
    [时钟]配置日期时间并同步到硬件
    [rhel]安装oracle11g
  • 原文地址:https://www.cnblogs.com/huanghzm/p/11046280.html
Copyright © 2020-2023  润新知