Java 9语言新特性
更新时间
浏览
TIP
本文主要是介绍 Java 9语言新特性 。
本文主要讲述一下Java9的新特性
# 特性列表
完整的特性详见JDK 9 features (opens new window),这里列几个相对重要的:
# 模块系统JPMS(重磅
)
相关的规范及JEP:
- Java Platform Module System (JSR 376) (opens new window)
- JEP 261: Module System (opens new window)
- JEP 200: The Modular JDK (opens new window)
- JEP 201: Modular Source Code (opens new window)
- JEP 282: jlink: The Java Linker (opens new window)
- JEP 220: Modular Run-Time Images (opens new window)
- JEP 260: Encapsulate Most Internal APIs (opens new window)
相关解读
- java9系列(三)模块系统精要 (opens new window)
- java9 opens与exports的区别 (opens new window)
- java9迁移注意事项 (opens new window)
- java9 module相关选项解析 (opens new window)
# G1成为默认垃圾回收器
相关JEP:
- JEP 248: Make G1 the Default Garbage Collector (opens new window)
- JEP 291: Deprecate the Concurrent Mark Sweep (CMS) Garbage Collector (opens new window)
- JEP 278: Additional Tests for Humongous Objects in G1 (opens new window)
相关解读
# Unified JVM/GC Logging
相关JEP:
- JEP 158: Unified JVM Logging (opens new window)
- JEP 264: Platform Logging API and Service (opens new window)
- JEP 271: Unified GC Logging (opens new window)
相关解读
# HTTP/2 Client(Incubator)
支持HTTP2,同时改进httpclient的api,支持异步模式。
相关JEP
相关解读
# jshell: The Java Shell (Read-Eval-Print Loop)
相关JEP
相关解读
# Convenience Factory Methods for Collections
相关JEP
以前大多使用Guava类库集合类的工厂,比如
Lists.newArrayList(1,2,3,4,5);
Sets.newHashSet(1,2,3,4,5);
Maps.newHashMap();
注意,上面这种返回的集合是mutable的
现在java9可以直接利用jdk内置的集合工厂,比如
List.of(1,2,3,4,5);
Set.of(1,2,3,4,5);
Map.of("key1","value1","key2","value2","key3","value3");
注意,jdk9上面这种集合工厂返回的是immutable的
# Process API Updates
相关JEP
相关解读
# Stack-Walking API
相关JEP
相关解读
# Variable Handles
相关JEP
相关解读
# docker方面支持
- Java SE support for Docker CPU and memory limits (opens new window)
- Docker CPU limits (opens new window)
- Experimental support for Docker memory limits (opens new window)
- Docker memory limits (opens new window)
# 其他
- JEP 238: Multi-Release JAR Files (opens new window)
- JEP 266: More Concurrency Updates (opens new window)
- JEP 274: Enhanced Method Handles (opens new window)
- JEP 295: Ahead-of-Time Compilation (opens new window)
# 小结
java9大刀阔斧,重磅引入了模块化系统,自身jdk的类库也首当其冲模块化。新引入的jlink可以精简化jdk的大小,外加Alpine Linux的docker镜像,可以大大减少java应用的docker镜像大小,同时也支持了Docker的cpu和memory限制(Java SE 8u131及以上版本开始支持
),非常值得使用。
# 参考文章
- https://segmentfault.com/a/1190000013620826