最近太忙了,只关注了上个月19号发布了java 21,也没有时间来写个总结。今天补上吧,欢迎大家关注➕ 点赞👍 加评论
介绍
这次发布的 JAVA 21 是个LTS版本所以大家可以放心食用,不过按当下JDK的更新速度,貌似LTS与否也不是特别重要了,毕竟明年三月底差不多要发布 JAVA 22 版本了。
新功能/特性 Features
430 String Templates (Preview)
这个新特性还算比较有意思, 可以直接写多行文本了,不需要再拼接了
String html = STR."""
<html>
<head>
<title>{title}</title>
</head>
<body>
<p>{text}</p>
</body>
</html>
""";
431 Sequenced Collections
这个🆕特性是在原有的集合collection基础上 加了几个高级抽象的sequence类
- SequencedCollection
- SequencedSet
- SequencedMap
441 Pattern Matching for switch
switch case 可以直接使用对象/类,省去了写很多if的麻烦
sealed interface CardClassification permits Suit, Tarot {}
public enum Suit implements CardClassification { CLUBS, DIAMONDS, HEARTS, SPADES }
final class Tarot implements CardClassification {}
static void exhaustiveSwitchWithoutEnumSupport(CardClassification c) {
switch (c) {
case Suit s when s == Suit.CLUBS -> {
System.out.println("It's clubs");
}
case Suit s when s == Suit.DIAMONDS -> {
System.out.println("It's diamonds");
}
case Suit s when s == Suit.HEARTS -> {
System.out.println("It's hearts");
}
case Suit s -> {
System.out.println("It's spades");
}
case Tarot t -> {
System.out.println("It's a tarot");
}
}
}
443 Unnamed Patterns and Variables (Preview)
这个还是看使用习惯吧
switch (b) {
case Box(RedBall _), Box(BlueBall _) -> processBox(b);
case Box(GreenBall _) -> stopProcessing();
case Box(_) -> pickAnotherBox();
}
444 Virtual Threads
虚拟线程这个是JAVA 21 最大的亮点
可以自己试一试,真的很香
现在支持虚拟线程的框架有:
- spring 6.1
- Quarkus 3 但是需要使用RunOnVirtualThread 注解, Micronaut4 也需要使用Execute(Blocking)注解, 这两个框架需要使用注解原因是原始程序是用netty写的
- Helidon 4 预计今年Q4 推出
支持的服务器 :
- tomcat 10.1.12, jetty12
支持的数据库:
- postgersql
- H2
- Oracle 21c
IDE 支持/使用
目前来说eclipse对java 21支持🆚intellij好一点
Intellij 预计在 2023.3 版本🀄️支持 java 21