经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
Java SE 20 新增特性
来源:cnblogs  作者:Grey Zeng  时间:2023/5/4 9:17:03  对本文有异议

Java SE 20 新增特性

作者:Grey

原文地址:

博客园:Java SE 20 新增特性

CSDN:Java SE 20 新增特性

源码

源仓库: Github:java_new_features

镜像仓库: GitCode:java_new_features

Switch类型匹配(第四次预览)

Java SE 17 新增特性中,Switch 类型匹配作为预览功能推出,到 Java SE 20 ,这个功能已经是第四次预览版,在 Java SE 17 中,可以通过加强 switch 表达式和语句的模式匹配能力,减少了定义这些表达式所需的模板,此外,switch 中增加了空值的支持。如下示例:

注:执行如下代码需要基于 Java SE 17 + ,且增加--enable-preview参数。

  1. package git.snippets.jdk20;
  2. /**
  3. * switch类型匹配(二次预览)
  4. *
  5. * @author <a href="mailto:410486047@qq.com">Grey</a>
  6. * @date 2023/05/03
  7. * @since 20
  8. */
  9. public class SwitchMatchTest {
  10. public static void main(String[] args) {
  11. switchMatch(3);
  12. switchMatch("HELLO");
  13. switchMatch("hello world");
  14. switchMatch(null);
  15. }
  16. static void switchMatch(Object obj) {
  17. switch (obj) {
  18. case String s when s.length() > 5 -> System.out.println(s.toUpperCase());
  19. case String s -> System.out.println(s.toLowerCase());
  20. case Integer i -> System.out.println(i * i);
  21. case null -> System.out.println("null obj");
  22. default -> {
  23. }
  24. }
  25. }
  26. }

范围值(Scoped Value,孵化阶段)

JEP 429 在 Java SE 20 的孵化阶段引入了范围值(ScopedValue), 范围值可以与虚拟线程很好地结合。它允许在有限的时间内存储一个值,而且只有写入该值的线程可以读取它。类似ThreadLocal对于线程的作用。详见:SCOPED VALUES IN JAVA

record 的匹配增强(第二次预览)

record 的匹配增强首次预览在 Java SE 19, record 可以与 instanceof 一起使用,也可以使用 switch 来访问记录的字段,而无需强制转换和调用访问器方法,一个 record 的示例如下

  1. package git.snippets.jdk20;
  2. /**
  3. * record 模式匹配增强(二次预览)
  4. * 需要增加 --enable-preview参数
  5. *
  6. * @author <a href="mailto:410486047@qq.com">Grey</a>
  7. * @date 2022/9/22
  8. * @since 19
  9. */
  10. public class RecordTest {
  11. public static void main(String[] args) {
  12. Points points = new Points(1, 2);
  13. Line line = new Line(new Points(1, 2), new Points(3, 4));
  14. printPoints(points);
  15. printLine(line);
  16. }
  17. private static void printPoints(Object object) {
  18. if (object instanceof Points(int x, int y)) {
  19. System.out.println("jdk 19 object is a position, x = " + x + ", y = " + y);
  20. }
  21. if (object instanceof Points points) {
  22. System.out.println("pre jdk 19 object is a position, x = " + points.x()
  23. + ", y = " + points.y());
  24. }
  25. switch (object) {
  26. case Points position -> System.out.println("pre jdk 19 object is a position, x = " + position.x()
  27. + ", y = " + position.y());
  28. default -> throw new IllegalStateException("Unexpected value: " + object);
  29. }
  30. switch (object) {
  31. case Points(int x, int y) -> System.out.println(" jdk 19 object is a position, x = " + x
  32. + ", y = " + y);
  33. default -> throw new IllegalStateException("Unexpected value: " + object);
  34. }
  35. }
  36. public static void printLine(Object object) {
  37. if (object instanceof Line(Points(int x1, int y1), Points(int x2, int y2))) {
  38. System.out.println("object is a path, x1 = " + x1 + ", y1 = " + y1
  39. + ", x2 = " + x2 + ", y2 = " + y2);
  40. }
  41. switch (object) {
  42. case Line(Points(int x1, int y1), Points(int x2, int y2)) ->
  43. System.out.println("object is a path, x1 = " + x1 + ", y1 = " + y1
  44. + ", x2 = " + x2 + ", y2 = " + y2);
  45. // other cases ...
  46. default -> throw new IllegalStateException("Unexpected value: " + object);
  47. }
  48. }
  49. }
  50. record Points(int x, int y) {
  51. }
  52. record Line(Points from, Points to) {
  53. }

此外,在 JEP 432 中,Java SE 20 的 record 支持类型推断,例如,定义了如下数据结构

  1. interface Multi<T> {}
  2. record Tuple<T>(T t1, T t2) implements Multi<T> {}
  3. record Triple<T>(T t1, T t2, T t3) implements Multi<T> {}

在 Java SE 20 之前,需要这样做

  1. // 需要指定类型
  2. static void preJDK20(Multi<String> multi) {
  3. if (multi instanceof Tuple<String>(var s1, var s2)) {
  4. System.out.println("Tuple: " + s1 + ", " + s2);
  5. } else if (multi instanceof Triple<String>(var s1, var s2, var s3)) {
  6. System.out.println("Triple: " + s1 + ", " + s2 + ", " + s3);
  7. }
  8. }

需要指定类型,例如:本实例需要指定 String 类型。

到了 Java SE 20,record 有类型推断,所以上述代码可以写成

  1. static void JDK20(Multi<String> multi) {
  2. if (multi instanceof Tuple(var s1, var s2)) {
  3. System.out.println("Tuple: " + s1 + ", " + s2);
  4. } else if (multi instanceof Triple(var s1, var s2, var s3)) {
  5. System.out.println("Triple: " + s1 + ", " + s2 + ", " + s3);
  6. }
  7. }

在循环中也可以支持类似的用法,示例如下:

在 Java SE 20 之前

  1. record Position(int x, int y) {
  2. }
  3. static void preJDK20Loop(List<Position> positions) {
  4. for (Position p : positions) {
  5. System.out.printf("(%d, %d)%n", p.x(), p.y());
  6. }
  7. }

在 Java SE 20 版本中,可直接写成如下形式

  1. static void JDK20Loop(List<Position> positions) {
  2. for (Position(int x, int y) : positions) {
  3. System.out.printf("(%d, %d)%n", x, y);
  4. }
  5. }

此外,在 Java SE 20 中,移除了对 record 命名模式的支持,在 Java SE 19 中,如下写法是对的

  1. if (object instanceof Points(int x, int y) points) {
  2. System.out.println("pre jdk 19 object is a position, x = " + points.x()
  3. + ", y = " + points.y());
  4. }

但是到了 Java SE 20 ,已经将上述写法废弃,Java SE 20 只支持如下两种写法

  1. if (object instanceof Points(int x, int y)) {
  2. System.out.println("jdk 19 object is a position, x = " + x + ", y = " + y);
  3. }
  4. if (object instanceof Points points) {
  5. System.out.println("pre jdk 19 object is a position, x = " + points.x()
  6. + ", y = " + points.y());
  7. }

废弃 java.net.URL 的构造方法

java.net.URL的构造函数已被标记为"废弃"。应该使用URI.create(..)URI.toURL()方法。下面是一个例子:

  1. package git.snippets.jdk20;
  2. import java.net.URI;
  3. import java.net.URL;
  4. /**
  5. * URL的构造方法被彻底废弃
  6. *
  7. * @author <a href="mailto:410486047@qq.com">Grey</a>
  8. * @date 2023/05/03
  9. * @since 20
  10. */
  11. public class URLConstructorTest {
  12. public static void main(String[] args) throws Exception {
  13. // 以下构造方法在 Java SE 20 被彻底废弃
  14. // URL url = new URL("xxx");
  15. // Java SE 20 用如下方法构造 URL
  16. URL url = URI.create("xxx").toURL();
  17. }
  18. }

更多

Java SE 7及以后各版本新增特性,持续更新中...

参考资料

Java Language Changes for Java SE 20

JDK 20 Release Notes

JAVA 20 FEATURES(WITH EXAMPLES)

原文链接:https://www.cnblogs.com/greyzeng/p/17249601.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号