jdk9新特性

1 模块化
  • jdk9增加模块化机制
  • 在模块提供通过exports指令需要提供三方使用对外暴漏
  • 模块使用通过requires指令需要依赖模块导入

  • jdk之前的rt.jar就拆分很多模块

2 jshell
  • jdk9新增jshell工具
  • jshell提供了一个交互式命令界面
  • 无需编译就可以直接执行代码

3 接口私有方法
  • 方便接口公共并且不想暴漏给子类方法提取
public interface HelloService {
    default  void test1(){
        System.out.println("test1");
        common();
    }
    default  void test2(){
        System.out.println("test2");
        common();
    }

    private void common(){
        System.out.println("common");
    }
}

4 改进 try-whith-resources
  • 允许资源try()外定义然后只要括号内指明需要自动关闭资源变量即可
public class App {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("C:\\jwork\\jdk9-demo\\module1\\src\\main\\java\\module-info.java");
        try (fis){
            byte[] buf = new byte[1024];
            int length = 0;
            while ((length=fis.read(buf))!=-1){
                System.out.println(new String(buf,0,length));
            }
        }
    }
}

5 改进砖石操作
  • 增强类型推断机制
  • 书写匿名内部类具有泛型只需要声明变量指定泛型类型即可

6 限制使用单独下划线标识符
7 String存储结构变更
  • string存储字符串Char数组替换成byte数组
  • 增加编码标识coder

8 快速创建只读集合
  • 以下方式创建集合只读
  • 新增或者修改抛异常
public class App {
    public static void main(String[] args) throws IOException {
        List<String> hello = List.of("hello", "World");
        Set<String> hello1 = Set.of("hello", "World");
        Map<Integer, String> integerStringMap = Map.of(1, "hello", 2, "World");
    }
}

9 增强Stream API
  • 新增方法dropWhile()遇到第一个不符合条件终止操作
  • 新增方法takeWhile(),遇到第一个不符合条件终止操作
  • 新增方法ofNullable()允许Stream放入null
public class App {
    public static void main(String[] args) throws IOException {
        List<String> hello = List.of("小青", "画画类","大神");
        hello.stream().dropWhile(s -> s.length() == 2).forEach(System.out::println);
        System.out.println("============================");
        hello.stream().takeWhile(s -> s.length() == 2).forEach(System.out::println);
        System.out.println("============================");
        Stream.ofNullable(null).forEach(System.out::println);
    }
}

10 改进Optional
  • 新增方法stream()
  • 新增方法ifPersentOrElse()
  • 新增方法or()
public class App {
    public static void main(String[] args) throws IOException {
        List<String> hello = List.of("小青", "画画类", "大神");
        Optional.of(hello).stream().forEach(System.out::println);
        System.out.println("============================");
        Optional<String> hello1 = Optional.ofNullable("hello");
        hello1.ifPresentOrElse(System.out::println, () -> {
            System.out.println("空空如也");
        });
        System.out.println("============================");
        Optional<String> hello2 = Optional.ofNullable(null);
        hello2.ifPresentOrElse(System.out::println, () -> {
            System.out.println("空空如也");
        });
        System.out.println("============================");
        Optional<String> hello3 = Optional.ofNullable("hello").or(() -> {
            return Optional.of("备用hello");
        });
        System.out.println(hello3.get());
        System.out.println("============================");
        Optional<Object> hello4 = Optional.ofNullable(null).or(() -> {
            return Optional.of("备用hello");
        });
        System.out.println(hello4.get());
    }
}

11 多分辨率图像APi
12 全新HTTP客户端API
  • 支持HTTP2
  • 自持websocket
public class App {
    public static void main(String[] args) throws IOException, InterruptedException {
        HttpClient.Builder builder = HttpClient.newBuilder();
        HttpClient client = builder.version(HttpClient.Version.HTTP_2)
                .executor(Executors.newFixedThreadPool(5))
                .followRedirects(HttpClient.Redirect.NEVER)
                .build();
        URI uri = URI.create("http://www.baidu.com");
        HttpRequest request = HttpRequest.newBuilder(uri).version(HttpClient.Version.HTTP_2).build();
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
        System.out.println(response.body());
    }
}

13 智能JAVA编译工具
  • 智能java编译工具sjavac第一阶段始于JEP139项目,用于多核处理器情况下提升JDK编译速度
  • 如今已经进入第二阶段JEP199其目的改进Java编译工具并取代javac
  • jdk9更新javac编译器以便java9代码运行版本java

14 统一JVM日志系统
15 java动态编译
  • JIT(Just-In-time)编译器可以运行时热点代码编译本地代码速度很快
  • 但是JIT需要花费较长时间才能热身完成
  • 优化java代码仍旧运行很慢
  • AoT编译就是为了解决上述问题而生
  • JDK9AOTJEP295:Ahead-of-Time Compilation)作为实验特性引入开发可以利用jaotc工具重点代码转换类库一样的文件
  • 使得Java应用虚拟机启动编译为原生代码旨在改进小型大型应用程序启动时间同时峰值性能影响很小
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值