1 Star 9 Fork 0

大漠飞天/java-8-to-21-features

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MulanPSL-2.0

java 8到21新特性一览,以及未来新特性展望

JDK下载

注:JDK17和JDK21是长期支持版本。JDK24是2025年3月发布的最新JDK,维护期是半年,如使用于生产环境,建议跟随JDK的节奏一路升。

java8(2014年)

大量新特性,主要有:

  • 函数式编程:lambda表达式、方法引用
  • 流式计算Stream
  • 可空容器Optional
  • 工具类:Files、Base64
  • CompletableFuture快捷异步工具
  • 默认接口方法
  • 全新时间api:LocalDate、LocalDateTime、Instant等。java.util.Date、SimpleDateFormat、Calender可以不用了。

全整示例见: com.jiayuan.feature.java8 运行单个单元测试即可体验用法

java11(2018年9月)

java8之后,java的版本发布方式改为小步快跑,允许一个实验性的预览项目提前与用户相见,但需要指定JVM参数 --enable-preview 才能使用。 java11是java8之后的一个长期支持版本,com.jiayuan.feature.java11 里包含了java9、java10、java11三个版本特性的用法,主要有:

  • 模块化:通过定义module-info.java来开启模块化功能,里面可申明要依赖的jdk模块、外部模块、开放反射写权限的包、SPI申明等
  • 构建List/Map更方便了:List.of、Map.of快速构建不可变List/Map。
  • try-with-resource里定义变量,实现了Autoclose接口的类,不用在末尾手动close()了
  • JDK内置了HttpClient,可告别apache httpclient、restTemplate、retrofit、okhttp等第三方http client包了。
  • 对Optional增强了ifPresentOrElse()等方法
  • 与操作系统有关的ProcessHandle、ProcessBuilder,能通过JDK来调用系统进程
  • MethodHandle、VarHandle:增强了方法句柄、变量句柄的功能,使用反射更便捷。
  • var:局部变量关键词,自动推断类型
  • 服务于Reactive Programming的全新Flow API:Publisher/Subscriber模型

java17(2021年9月)

java11之后,JCP(即Java社区委员会)正式确定每半年一个非长期支持版本,主要目的是新特性可以更快发布出来,有兴趣的开发者可以提早尝鲜新特性,并反馈使用效果和意见至Java社区,在后面的版本做升级。 每3年一个长期支持版本(LTS)。Java17是LTS,com.jiayuan.feature.java17 包含了java12、java13、java14、java15、java16、java17这6个版本特性的用法,主要有:

  • 文本块:多行文本无需加号和双引号拼接,多行文本更方便,代码里的文本块所见即所得,更整洁。
  • record类:记录类,只读,不可修改属性值,告别getter/setter/lombok,一行代码即可申明一个对象,可用于数据对象申明。
  • sealed class:密封类,子类可被枚举,仅对指类的子类开放继承权、实现权。
  • 明确的空指针对象:抛异常时,会告知具体是哪个对象为null导致的NullPointerException
  • switch表达式:写法更精简,还可以做为赋值语句把整个switch当成一个值。
  • 模式匹配:模式匹配后支持变量定义,告别cast强转
  • Stream to List更简单
  • ZGC:Zero GC,号称零卡顿垃圾回收器

java21(2023年9月)

java17之后,经JCP会员投票,Java的版本3年一个LTS太久了,改为了两年一个LTS。java17之后的LTS就是21,21之后的LTS是将在2025年9月发布的Java25。 com.jiayuan.feature.java21 包含了java18、java19、java20、java21这4个版本特性的用法,主要有:

  • 虚拟线程:轻量级线程,也称java版协程,性能不低于go的协程,大幅提升IO吞吐型互联网应用的并发能力。消耗很小的资源即可进行多线程编程,无需池化,告别线程池数量调参
  • switch里支持模式匹配、支持对record模式匹配
  • List取第一个、最后一个元素、反转List更方便了
  • SimpleFileServer:轻量级http web server,可做java版nginx,支持本地二级目录路由、反向代理等。
  • 分代ZGC:对ZGC进行了分代回收。

java22(2024年3月)

  • Foreign Function & Memory API:增强了Java跨语言能力,可替代JNI来调C/C++编译的本地库
  • 其他未正式发布的预览特性,见:https://openjdk.org/projects/jdk/22/

java23(2024年9月)

java24(2025年3月)

本版本特性很多,大部分是底层优化,语法及开发者主要关注的有:

  • Class-File API:正式特性,可用来取代第三方库asm的字节码操作
  • Stream Gatherers: 允许对流进行窗口操作(如滑动窗口、跳跃窗口等),适用场景:时间序列分析、文本处理、统计连续N个单词的字母数、信号处理里滑动窗口滤波等
  • 虚拟线程里使用synchronized不再被pinning:Java21发布了虚拟线程,但对于老旧的第三方库里如果使用了synchronized关键词,在某些情况下会导致虚拟线程被平台线程pin住,24里已经修复此问题。
  • 更多详见:https://openjdk.org/projects/jdk/24/

Java社区正在研发中的系例项目

  • Project Amber:对Java语法进行现代化提升,让java为更简洁、更强表达力,已交付的:模式匹配、switch表达式、字符串模板(回炉改造中)等。在研发的:with 赋值符(简化对象拷贝赋值)...
  • Project Loom:关注于并发编程的性能提升,已交付的:虚拟线程、scoped value(第四版预览)、结构化并发(第四版预览)
  • Project Babylon:主要研究:增强反射能力、通过表达式树实现Linq、GPU运算、自动微分、提升数学计算、机器学习等。见: https://openjdk.org/projects/babylon/
  • Project Graal:研究Java AOT编译。支持java字节码编译为native二进制可执行文件;用java写javac、JVM;支持Javascript/Python等语言编译为java字节码等。
  • Project Valhalla: 值对象、空安全、泛型免装箱拆箱 https://openjdk.org/projects/valhalla/
  • Project Panama:增强Java与native的交互能力。主要有:原生高性能向量API、FFM、FFI...
  • Project Lilliput:缩减Java对象头大小,由96/128减少至64,内存可节省33%至50%,此特性已随Java24发布(实验特性),需加参数开启
  • Project Leyden:提升JVM启动性能,缩短预热时间,研究 JIT & AOT 结合编译
木兰宽松许可证,第2版 木兰宽松许可证,第2版 2020年1月 http://license.coscl.org.cn/MulanPSL2 您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束: 0. 定义 “软件” 是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。 “贡献” 是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。 “贡献者” 是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。 “法人实体” 是指提交贡献的机构及其“关联实体”。 “关联实体” 是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是 指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。 1. 授予版权许可 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可 以复制、使用、修改、分发其“贡献”,不论修改与否。 2. 授予专利许可 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定 撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡 献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软 件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“ 关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或 其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权 行动之日终止。 3. 无商标许可 “本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定 的声明义务而必须使用除外。 4. 分发限制 您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“ 本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。 5. 免责声明与责任限制 “软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对 任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于 何种法律理论,即使其曾被建议有此种损失的可能性。 6. 语言 “本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文 版为准。 条款结束 如何将木兰宽松许可证,第2版,应用到您的软件 如果您希望将木兰宽松许可证,第2版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步: 1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字; 2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中; 3, 请将如下声明文本放入每个源文件的头部注释中。 Copyright (c) [Year] [name of copyright holder] [Software Name] is licensed under Mulan PSL v2. You can use this software according to the terms and conditions of the Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2 THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. Mulan Permissive Software License,Version 2 Mulan Permissive Software License,Version 2 (Mulan PSL v2) January 2020 http://license.coscl.org.cn/MulanPSL2 Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions: 0. Definition Software means the program and related documents which are licensed under this License and comprise all Contribution(s). Contribution means the copyrightable work licensed by a particular Contributor under this License. Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License. Legal Entity means the entity making a Contribution and all its Affiliates. Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity. 1. Grant of Copyright License Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not. 2. Grant of Patent License Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken. 3. No Trademark License No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in section 4. 4. Distribution Restriction You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software. 5. Disclaimer of Warranty and Limitation of Liability THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 6. Language THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL. END OF THE TERMS AND CONDITIONS How to Apply the Mulan Permissive Software License,Version 2 (Mulan PSL v2) to Your Software To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps: i. Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner; ii. Create a file named "LICENSE" which contains the whole context of this License in the first directory of your software package; iii. Attach the statement to the appropriate annotated syntax at the beginning of each source file. Copyright (c) [Year] [name of copyright holder] [Software Name] is licensed under Mulan PSL v2. You can use this software according to the terms and conditions of the Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2 THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v2 for more details.

简介

java-8-to-21-features 展开 收起
README
MulanPSL-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/jayung/java-8-to-21-features.git
git@gitee.com:jayung/java-8-to-21-features.git
jayung
java-8-to-21-features
java-8-to-21-features
master

搜索帮助