Java23 ta来了,你发任你发,我用Java8

前言

Java 23 来啦!和Java 22 一样,这也是一个非 LTS(长期支持)版本,Oracle 仅提供六个月的支持。下一个长期支持版是 Java25,预计明年 9 月份发布。

JDK 23 提供了 12 项增强功能,这些增强功能的重要性足以保证他们自己的 JDK 增强提案 - JEP,包括 8 项预览功能和 1 项孵化器功能。 它们涵盖了对 Java 语言、API、性能和 JDK 中包含的工具的改进。除了在 Java 平台上的工作之外,在 Oracle JDK 23 中,Oracle GraalVM JIT 编译器 (Graal JIT) 现在包含在作为 Oracle JDK 一部分提供的 JIT 中。

  • JEP455:模式中的原始类型、instanceof和switch(预览)
  • JEP456:类文件API(第二次预览)
  • JEP467:Markdown文档注释
  • JEP469:向量API(第八次孵化)
  • JEP473:流收集器(第二次预览)
  • JEP471:弃用sun.misc.Unsafe中的内存访问方法
  • JEP474:ZGC:默认的分代模式
  • JEP476:模块导入声明(预览)
  • JEP477:未命名类和实例main方法(第三次预览)
  • JEP480:结构化并发(第三次预览》
  • JEP481:作用域值(第三次预览)
  • JEP482:灵活的构造函数体(第二次预览)

JEP 467:Markdown 文档注释

允许使用 Markdown 编写 Javadoc 注释,使文档编写更易读、易写。相比之前的 HTML 编写方式,Markdown 更受开发者欢迎。

 

java

代码解读

复制代码

/** /// return the ceiling modulus of the `long` and `int` arguments. /// /// the ceiling modulus is `r = x – (ceildiv(x, y) * y)`, /// has the opposite sign as the divisor `y` or is zero, and /// is in the range of `-abs(y) < r < +abs(y)`. /// /// the relationship between `ceildiv` and `ceilmod` is such that: /// /// – `ceildiv(x, y) * y + ceilmod(x, y) == x` /// /// for examples, see (#ceilmod(int, int)). /// /// @param x the dividend /// @param y the divisor /// @return the ceiling modulus `x – (ceildiv(x, y) * y)` /// @throws ArithmeticException if the divisor `y` is zero /// @see #ceildiv(long, int) /// @since 18 */ public class MathFunctions { // 方法的具体实现 }

以上是传统的 Javadoc 注释写法,如果使用 Markdown 来写,如下所示:

 

java

代码解读

复制代码

/// return the ceiling modulus of the `long` and `int` arguments. /// /// the ceiling modulus is `r = x – (ceildiv(x, y) * y)`, /// has the opposite sign as the divisor `y` or is zero, and /// is in the range of `-abs(y) < r < +abs(y)`. /// /// the relationship between `ceildiv` and `ceilmod` is such that: /// /// – `ceildiv(x, y) * y + ceilmod(x, y) == x` /// /// for examples, see (#ceilmod(int, int)). /// /// @param x the dividend /// @param y the divisor /// @return the ceiling modulus `x – (ceildiv(x, y) * y)` /// @throws ArithmeticException if the divisor `y` is zero /// @see #ceildiv(long, int) /// @since 18

注意,使用 Markdown 注释时需要用三个斜杠(///)开始 Javadoc 注释的所有行。

一下是两种模式的区别 

image

(JEP 474)ZGC:默认的分代模式

Z 垃圾回收器 (ZGC) 的默认模式切换为分代模式,并弃用非分代模式,计划在未来版本中移除。这是因为分代 ZGC 是大多数场景下的更优选择。

(JEP 455)模式、instanceof 和 switch 中的基本数据类型匹配(预览)

可以在模式、instanceofswitch语句中使用基本数据类型来匹配变量,增强了代码的表达能力和灵活性,使代码更简洁、易读。

 

java

代码解读

复制代码

class DataTypeMatchingExample { public static void main(String[] args) { // 使用模式匹配判断基本数据类型 int num = 10; if (num instanceof Integer i) { System.out.println("It's an integer: " + i); } // 使用 switch 语句对基本数据类型进行匹配 switch (num) { case 10 -> System.out.println("The number is 10"); default -> System.out.println("It's a different number"); } } }

(JEP 482)灵活的构造函数体(第二预览)

在调用super(...)之前,允许定义其他语句,使用灵活的构造函数体来初始化构造函数中的字段,为构造函数的编写提供了更大的灵活性。

 

java

代码解读

复制代码

class ParentClass { private int parentField; public ParentClass(int parentField) { this.parentField = parentField; System.out.println("Parent class constructor called."); } } class ChildClass extends ParentClass { private int childField; public ChildClass(int parentField, int childField) { // 在调用 super 之前初始化 childField this.childField = childField; super(parentField); System.out.println("Child class constructor called."); } }

(JEP 476)模块导入声明(预览)

增强了 Java 的功能,使其能够简洁地导入模块导出的所有软件包。简化了模块库的使用,避免了使用模块导出的 API 的不同部分时产生多个类型导入声明的麻烦,也让初学者更轻松地使用第三方库和基本 Java 类。

 

java

代码解读

复制代码

// 假设存在一个名为 "myModule" 的模块,其中包含一个 "com.example" 包 import myModule.*; public class ModuleImportExample { public static void main(String[] args) { // 可以直接使用 "com.example" 包中的类 com.example.SomeClass someObject = new com.example.SomeClass(); // 后续代码操作 } }

(JEP 473)流收集器(第二次预览)

增强了流 API 以支持自定义中间操作,允许流管道以现有内置中间操作无法轻易实现的方式转换数据,使流管道更加灵活和富有表现力,并且可以操纵无限大小的流。

 

java

代码解读

复制代码

import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class StreamGatherersExample { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); // 使用流收集器进行自定义的转换操作 List<String> result = numbers.stream() .collect(Collectors.gatheringAndThen( Collectors.mapping(n -> "Number: " + n, Collectors.toList()), list -> list.stream().sorted().collect(Collectors.toList()) )); result.forEach(System.out::println); } }

(JEP 466)类文件 API

提供处理类文件的 API,遵循 Java 虚拟机规范定义的类文件格式,使 JDK 组件能够迁移到标准 API,并最终删除 JDK 内部的第三方 ASM 库副本。该 API 包含了简化的类 CodeBuilder,具有用于字节码指令的工厂方法,包括低级工厂、中级工厂和基本块的高级构建器等改进功能。

(JEP 469)矢量 API(第八轮孵化)

引入了一种 API 来表达矢量计算,这些计算可在运行时可靠地编译为受支持的 CPU 架构上的最佳矢量指令,旨在提供清晰简洁的 API、在不同架构上提供可靠的运行时编译和性能、提供优雅降级、与平台无关以及与 Valhalla 项目保持一致。

 

java

代码解读

复制代码

import jdk.incubator.vector.VectorSpecies; import jdk.incubator.vector.FloatVector; public class VectorAPIDemo { public static void main(String[] args) { // 获取默认的矢量类型 VectorSpecies<Float> species = FloatVector.SPECIES_PREFERRED; float[] a = {1.0f, 2.0f, 3.0f, 4.0f}; float[] b = {5.0f, 6.0f, 7.0f, 8.0f}; float[] c = new float[4]; int i = 0; int upperBound = species.loopBound(a.length); for (; i < upperBound; i += species.length()) { // 从数组创建矢量 var va = FloatVector.fromArray(species, a, i); var vb = FloatVector.fromArray(species, b, i); // 执行矢量计算 var vc = va.mul(va).add(vb.mul(vb)).neg(); // 将结果写回到数组 vc.intoArray(c, i); } for (; i < a.length; i++) { c[i] = (a[i] * a[i] + b[i] * b[i]) * -1.0f; } // 打印结果 for (float f : c) { System.out.println(f); } } }

隐式声明类和实例主方法(第三预览)

改进了 Java 语言,使初学者无需了解为大型程序设计的语言功能即可快速编写自己的第一批程序,并且添加了自动导入用于控制台简单文本 I/O 的方法以及按需导入 static java.base 模块导出的包和所有公共顶级类与接口的功能。

 

java

代码解读

复制代码

// 隐式声明的类,无需显式的类定义 public class ImplicitClassExample { public static void main(String[] args) { // 直接使用隐式导入的方法进行简单的控制台输出 System.out.println("This is an example of an implicitly declared class."); } }

(JEP 444)虚拟线程

虚拟线程是轻量级的线程,可以大量创建而不会给系统资源带来巨大压力。它们旨在简化高并发应用程序的开发,使编写异步代码更像编写同步代码。

 

java

代码解读

复制代码

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class VirtualThreadExample { public static void main(String[] args) { // 创建一个可以创建虚拟线程的执行器 ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); for (int i = 0; i < 10; i++) { int taskId = i; // 提交任务到执行器,每个任务在一个虚拟线程中运行 executor.submit(() -> { System.out.println("Task " + taskId + " is running on a virtual thread."); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }); } // 关闭执行器 executor.shutdown(); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值