Java SE 21 新增特性
Java SE 21 新增特性
作者:Grey
原文地址:
源码
镜像仓库: GitCode:java_new_features
Record Patterns
该功能首次在 Java SE 19 中预览,在Java SE 20中发布第二次预览版,在此版本中成为永久性功能。这意味着它可以在任何为 Java SE 21 编译的程序中使用,而无需启用预览功能。
示例代码如下
package git.snippets.jdk21;
/**
* record 模式匹配增强
* 无须增加 --enable-preview参数
*
* @author <a href="mailto:410486047@qq.com">Grey</a>
* @date 2023/09/25
* @since 21
*/
public class RecordTest {
public static void main(String[] args) {
Points points = new Points(1, 2);
Line line = new Line(new Points(1, 2), new Points(3, 4));
printPoints(points);
printLine(line);
}
private static void printPoints(Object object) {
if (object instanceof Points(int x, int y)) {
System.out.println("jdk 19 object is a position, x = " + x + ", y = " + y);
}
if (object instanceof Points points) {
System.out.println("pre jdk 19 object is a position, x = " + points.x()
+ ", y = " + points.y());
}
switch (object) {
case Points position -> System.out.println("pre jdk 19 object is a position, x = " + position.x()
+ ", y = " + position.y());
default -> throw new IllegalStateException("Unexpected value: " + object);
}
switch (object) {
case Points(int x, int y) -> System.out.println(" jdk 19 object is a position, x = " + x
+ ", y = " + y);
default -> throw new IllegalStateException("Unexpected value: " + object);
}
}
public static void printLine(Object object) {
if (object instanceof Line(Points(int x1, int y1), Points(int x2, int y2))) {
System.out.println("object is a path, x1 = " + x1 + ", y1 = " + y1
+ ", x2 = " + x2 + ", y2 = " + y2);
}
switch (object) {
case Line(Points(int x1, int y1), Points(int x2, int y2)) ->
System.out.println("object is a path, x1 = " + x1 + ", y1 = " + y1
+ ", x2 = " + x2 + ", y2 = " + y2);
// other cases ...
default -> throw new IllegalStateException("Unexpected value: " + object);
}
}
}
record Points(int x, int y) {
}
record Line(Points from, Points to) {
}
Switch 匹配增强
该功能首次在 Java SE 17 中预览,在在此版本中成为永久性功能。这意味着它可以在任何为 Java SE 21 编译的程序中使用,而无需启用预览功能。
package git.snippets.jdk21;
/**
* switch类型增强匹配
* 无须增加预览参数
*
* @author <a href="mailto:410486047@qq.com">Grey</a>
* @date 2023/09/25
* @since 21
*/
public class SwitchMatchTest {
public static void main(String[] args) {
switchMatch(3);
switchMatch("HELLO");
switchMatch("hello world");
switchMatch(null);
}
static void switchMatch(Object obj) {
switch (obj) {
case String s when s.length() > 5 -> System.out.println(s.toUpperCase());
case String s -> System.out.println(s.toLowerCase());
case Integer i -> System.out.println(i * i);
case null -> System.out.println("null obj");
default -> {
}
}
}
}
String template(预览功能)
作为本版本的预览功能推出。字符串模板是对 Java 现有字符串字面量和文本块的补充,它将字面文本与嵌入式表达式和模板处理器结合起来,从而产生专门的结果。
在 Java SE 21之前,字符串的拼接可以用下述三种方式
public static void stringTestBefore21() {
int a = 1;
int b = 2;
String concatenated = a + " times " + b + " = " + a * b;
String format = String.format("%d times %d = %d", a, b, a * b);
String formatted = "%d times %d = %d".formatted(a, b, a * b);
System.out.println(concatenated);
System.out.println(format);
System.out.println(formatted);
}
Java SE 21可以用更直观的方法实现字符串的拼接
public static void stringTest21() {
int a = 1;
int b = 2;
String interpolated = STR. "\{ a } times \{ b } = \{ a * b }" ;
System.out.println(interpolated);
String dateMessage = STR. "Today's date: \{
LocalDate.now().format(
// We could also use DateTimeFormatter.ISO_DATE
DateTimeFormatter.ofPattern("yyyy-MM-dd")
) }" ;
System.out.println(dateMessage);
int httpStatus = 200;
String errorMessage = "error pwd";
String json = STR. """
{
"httpStatus": \{ httpStatus },
"errorMessage": "\{ errorMessage }"
}""" ;
System.out.println(json);
}
Unnamed Patterns and Variables(预览功能)
作为预览功能引入,未命名模式匹配一个记录组件,但不声明组件的名称或类型。未命名变量是可以初始化但不使用的变量,都可以使用下划线字符 (_) 来表示它们。
例如:
try {
int number = Integer.parseInt(string);
} catch (NumberFormatException e) {
System.err.println("Not a number");
}
其中 e 是未使用的变量,可以写成
try {
int number = Integer.parseInt(string);
} catch (NumberFormatException _) {
System.err.println("Not a number");
}
再如
Object object = null;
if (object instanceof Points(int x, int y)) {
System.out.println("object is a position, x = " + x);
}
其中 y 是未使用的变量,可以写成
Object object = null;
if (object instanceof Points(int x, int _)) {
System.out.println("object is a position, x = " + x);
}
switch 表达式中也可以有类似的用法,例如
Object obj = null;
switch (obj) {
case Byte b -> System.out.println("Integer number");
case Short s -> System.out.println("Integer number");
case Integer i -> System.out.println("Integer number");
case Long l -> System.out.println("Integer number");
case Float f -> System.out.println("Floating point number");
case Double d -> System.out.println("Floating point number");
default -> System.out.println("Not a number");
}
也可以写成
Object obj = null;
switch (obj) {
case Byte _ -> System.out.println("Integer number");
case Short _ -> System.out.println("Integer number");
case Integer _ -> System.out.println("Integer number");
case Long _ -> System.out.println("Integer number");
case Float _ -> System.out.println("Floating point number");
case Double _ -> System.out.println("Floating point number");
default -> System.out.println("Not a number");
}
Unnamed Classes and Instance Main Methods (预览功能)
预览功能,简言之,就是main方法可以更加精简,原先写一个 Hello World 需要这样做
public class UnnamedClassesAndInstanceMainMethodsTest {
public static void main(String[] args) {
System.out.println("hello world");
}
}
现在可以简化成
public class UnnamedClassesAndInstanceMainMethodsTest {
void main() {
System.out.println("hello world");
}
}
注:上述代码需要在命令行运行,目前IDE还不支持,在命令行下执行:
java --enable-preview --source 21 UnnamedClassesAndInstanceMainMethodsTest.java
输出:hello world
更多
参考资料
Java Language Changes for Java SE 21
JAVA 21 FEATURES(WITH EXAMPLES
Java SE 21 新增特性的更多相关文章
- Java SE 8 新增特性
Java SE 8 新增特性 作者:Grey 原文地址: Java SE 8 新增特性 源码 源仓库: Github:java_new_features 镜像仓库: GitCode:java_new_ ...
- Java SE 15 新增特性
Java SE 15 新增特性 作者:Grey 原文地址:Java SE 15 新增特性 源码 源仓库: Github:java_new_features 镜像仓库: GitCode:java_new ...
- Java SE 16 新增特性
Java SE 16 新增特性 作者:Grey 原文地址:Java SE 16 新增特性 源码 源仓库: Github:java_new_features 镜像仓库: GitCode:java_new ...
- Java SE 17 新增特性
Java SE 17 新增特性 作者:Grey 原文地址:Java SE 17 新增特性 源码 源仓库: Github:java_new_features 镜像仓库: GitCode:java_new ...
- Java SE 9 新增特性
Java SE 9 新增特性 作者:Grey 原文地址: Java SE 9 新增特性 源码 源仓库: Github:java_new_features 镜像仓库: GitCode:java_new_ ...
- Java SE 10 新增特性
Java SE 10 新增特性 作者:Grey 原文地址:Java SE 10 新增特性 源码 源仓库: Github:java_new_features 镜像仓库: GitCode:java_new ...
- Java SE 11 新增特性
Java SE 11 新增特性 作者:Grey 原文地址:Java SE 11 新增特性 源码 源仓库: Github:java_new_features 镜像仓库: GitCode:java_new ...
- Java SE 12 新增特性
Java SE 12 新增特性 作者:Grey 原文地址:Java SE 12 新增特性 源码 源仓库: Github:java_new_features 镜像仓库: GitCode:java_new ...
- Java SE 13 新增特性
Java SE 13 新增特性 作者:Grey 原文地址:Java SE 13 新增特性 源码 源仓库: Github:java_new_features 镜像仓库: GitCode:java_new ...
- Java SE 14 新增特性
Java SE 14 新增特性 作者:Grey 原文地址:Java SE 14 新增特性 源码 源仓库: Github:java_new_features 镜像仓库: GitCode:java_new ...
随机推荐
- WWDC2023 Session系列:探索XCode15新特性
一.版本说明 XCode 15 beta 发布于 2023 年 6月5日, 可支持 macOS 13.3 或以上版本, 你可以按需下载需要的平台. 二.新增特性 1.代码智能提示 (Code comp ...
- SpringBoot 如何优雅的进行全局异常处理?
在SpringBoot的开发中,为了提高程序运行的鲁棒性,我们经常需要对各种程序异常进行处理,但是如果在每个出异常的地方进行单独处理的话,这会引入大量业务不相关的异常处理代码,增加了程序的耦合,同时未 ...
- java.lang.IndexOutOfBoundsException
原因:一个ArrayList数组中没有元素,而你想获取第一个元素,运行是就会报此类型的错误 解决方案:用 array[] 的 .length 查看 数组的长度
- SQL专家云回溯某时间段内的阻塞
背景 SQL专家云像"摄像头"一样,对环境.参数配置.服务器性能指标.活动会话.慢语句.磁盘空间.数据库文件.索引.作业.日志等几十个运行指标进行不同频率的实时采集,保存到SQL专 ...
- Kali下载安装以及基础配置
Kali官网:Kali Linux | Penetration Testing and Ethical Hacking Linux Distribution Kali下载地址:Get Kali | K ...
- vue3 安装 3d-force-graph
1.首先创建vue3的项目 2.创建好后通过开发工具打开项目并打开命令行,输入指令 npm install 3d-force-graph 安装即可 3.在使用的页面中引入 3d-force-graph ...
- 【Jenkins】 GitLab Gitee GitHub 部署
Jenkins GitLab Gitee GitHub 部署 环境 Jenkins Git Maven Jenkins 部署可参考文章:https://www.cnblogs.com/cxt618/p ...
- python开发之远程开发工具对比
前言 除了本地开发外,还有一种常见的开发方式就是远程开发,一般情况是一台Windows或mac笔记本作为日常使用的电脑,另有一台linux服务器作为开发服务器.开发服务器的性能往往较强,这样远程开发的 ...
- 工作笔记--简单网线连接,使另一台设备通过笔记本电脑的wifi上网
条件 笔记本电脑能够通过 wifi 上网 另一台与笔记本电脑网线连接正常 配置固定 ip,使之可以互相 ping 通 操作 点击右下角 wifi 图标,点击[网络和 Internet 设置] 在高级网 ...
- 层叠样式表(CSS)1
一.css的简介 1.层叠样式表的含义 层叠样式表:css是不仅是表现HTML的语言.还是进行样式修饰的语言 层叠:是对一个元素多次设置同一个样式,层层叠加覆盖,如不同的样式对一html标签进行修饰, ...