JDK
JDK17
1.同时配置Java8和17环境
用户变量
2.新特性
public static void main(String[] args) {
// 1.文本框
String text = """
{
"name": "小黑说Java",
"age": 18,
"address": "北京市西城区"
}
""";
System.out.println(text);
// 2.Switch
String f = "a";
switch (f) {
case "a" -> System.out.println("a");
case "b" -> System.out.println("b");
default -> System.out.println("c");
}
// 返回值
String res = switch (f) {
case "a" -> "a";
case "b" -> "b";
default -> "c";
};
System.out.println(res);
// 做多件事 yield返回
String res1 = switch (f) {
case "a" -> {
System.out.println("a");
yield "a";
}
case "b" -> "b";
default -> "c";
};
System.out.println(res1);
// 3.record关键字 用于创建不可变的数据类
Person person1 = new Person("lwx1");
Person person2 = new Person("lwx2");
record PersonRecord(String name) {
// 构造方法
PersonRecord {
System.out.println("PersonRecord构造方法");
}
}
PersonRecord personRecord1 = new PersonRecord(person1.getName());
PersonRecord personRecord2 = new PersonRecord(person2.getName());
System.out.println(personRecord1);
System.out.println(personRecord2);
// 4.sealed class 密封类
// 更好的控制哪些类可以对我定义的类进行扩展
// 可以控制有哪些类可以对超类进行继承,
// 5.instanceof 模式匹配
// 对一个变量的类型进行判断,如果符合指定的类型,则强制类型转换为一个新变量。
Object o = new Object();
if (o instanceof Fruit fruit) {
System.out.println("This furit is :" + fruit.getName);
}
// 6.Helpful NullPointerExceptions
// 准确显示发生NPE的精确位置
// 7.日期周期格式化 指示一天时间段
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("B");
System.out.println(dtf.format(LocalTime.of(8, 0)));
System.out.println(dtf.format(LocalTime.of(13, 0)));
System.out.println(dtf.format(LocalTime.of(20, 0)));
System.out.println(dtf.format(LocalTime.of(23, 0)));
System.out.println(dtf.format(LocalTime.of(0, 0)));
// 8.精简数字格式化支持
NumberFormat fmt = NumberFormat.getCompactNumberInstance(Locale.ENGLISH, NumberFormat.Style.SHORT);
System.out.println(fmt.format(1000));
System.out.println(fmt.format(100000));
System.out.println(fmt.format(1000000));
// 9.Stream.toList()
Stream<String> stringStream = Stream.of("a", "b", "c");
List<String> stringList = stringStream.toList();
for(String s : stringList) {
System.out.println(s);
}
}
- 密封类
// 不想让Fruit在该包以外被扩展 通过关键字sealed声明为密封类,通过permits可以指定Apple,Pear类可以进行继承扩展。
// 子类指明是final,non-sealed或sealed的
public abstract sealed class Fruit permits Apple {
}
public non-sealed class Apple extends Fruit {
}
JDK21
1.虚拟线程
更高效,更轻量级的线程模型
解决线程频繁切换和并发量的问题
2.Sequedced Collections
适用于此类集合的统一操作集
3.ZGC分代收集
拓展ZGC来提高性能,区分年轻代和老年代
4.准备禁用动态代理
将代理动态记载到JVM将发出警告,存在潜在安全风险
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2022-10-04 自定义组件
2022-10-04 OFF12 二维数组路径
2022-10-04 OFF14
2022-10-04 快速幂