part3 jdk17新特性详解
-
switch case 匹配
-
多行字符串处理
public class htmlHandler { public static void main(String[] args) { //jdk17处理多行字符方式 String html2= """ <html>\ <body>\s <h1>helloworld</h1> </body> </html> """; } } jdk17提供了两个转义符: \ 置于行尾 转成一行 \s 单个空白字符
-
instance of 增强
jdk17之前,需要强转
-
jdk17之后不需要类型强转
public class InstanceOf {
public static void main(String[] args) {
Object o=2;
if(o instanceof Integer i){
System.out.println(i.intValue());
}
}
}
-
密封类/限制继承类(sealed class) :
- 限制子类继承,防止子类变父类;
- sealed修饰在父类或者接口上;
- 子类必须使用final或non-sealed修饰 ;
- 密封类的子类或父类必须放在同一个包中
父接口:
public abstract sealed class Animal permits Dog, Cat { }
子接口:
non-sealed修饰的子类可以被子类继承 package com.newfeature; import com.newfeature.Animal; public non-sealed class Dog extends Animal { } package com.newfeature.domain; import com.newfeature.Dog; public class HaShiQi extends Dog { } final修饰的子类无法被继承 package com.newfeature; import com.newfeature.Animal; public final class Cat extends Animal { }
密封类的优点:1.更加安全 ,避免不必要的继承; 2.更加可控,可以限制类的包范围
- Record类,类似lombok的属性只读对象;提供全参的构造方法,以及只读的get方法,没有set
package com.newfeature;
public record RecordUser(Long userId,String userName) {
}
package com.newfeature;
public class RecordDemo {
public static void main(String[] args) {
RecordUser recordUser=new RecordUser(1L,"heihe");
RecordUser recordUser1=new RecordUser(1L,"heihe");
System.out.println("获取属性username:"+recordUser.userName()+","+recordUser.equals(recordUser1));
}
}
说明:该equal方法是record重新的方法,因此对象比较,内容相同,返回结果为true.
输出:
获取属性username:heihe,true
6.优化空指针异常信息:告诉哪个对象空指针了
创建类Dept,person
package com.newfeature;
import com.newfeature.domain.Person;
import lombok.Data;
@Data
public class Dept {
private Integer DeptNo;
private Person person;
}
package com.newfeature.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String name;
private Integer age;
private Integer height;
}
创建测试类
package com.newfeature;
public class NullPointerDemo {
public static void main(String[] args) {
Dept dept=new Dept();
System.out.println(dept.getPerson().getAge());
}
}
输出结果:
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3\lib\idea_rt.jar=54479:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3\bin" -Dfile.encoding=UTF-8 -classpath D:\ideaworkplace\ag\target\classes;C:\Users\Lenovo\.m2\repository\org\projectlombok\lombok\1.18.28\lombok-1.18.28.jar com.newfeature.NullPointerDemo
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.newfeature.domain.Person.getAge()" because the return value of "com.newfeature.Dept.getPerson()" is null
at com.newfeature.NullPointerDemo.main(NullPointerDemo.java:8)
Process finished with exit code 1
-
ZGC垃圾收集器:
-
垃圾回收不卡顿
-
垃圾回收stop the world小于10毫秒,堆内存可以设置很大,甚至是T级别的,减少gc的次数
-
优点:ZGC吞吐量高,性能高,低延迟 相对jdk8/11
使用方法: -xx:+useZGC
jdk8/11默认是 G1垃圾回收器
8.重写socket底层API
9.jdk飞行记录事件流(JFR Event):记录系统cpu资源等使用情况
10.EdDSA签名算法(爱德华兹曲线数字签名算法):Edwards-Curve Digital Signature Algorithm
原文地址:https://blog.csdn.net/crazy123456789/article/details/135862985
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!