入群修改网站后台,企业咨询服务是做什么的,wordpress 被写入文件,建设京东类的网站需要什么流程文章目录 前言一 #xff1a; yield关键字二 #xff1a;var关键字三 #xff1a;密封类四 #xff1a;空指针异常#xff1a;五#xff1a;接口中的私有方法#xff1a;六#xff1a;instanceof关键字 前言
这里介绍jdk17相对于jdk1.8的部分新增特性。
一 #xff… 文章目录 前言一 yield关键字二 var关键字三 密封类四 空指针异常五接口中的私有方法六instanceof关键字 前言
这里介绍jdk17相对于jdk1.8的部分新增特性。
一 yield关键字
yield关键字用于switch语句简写的场景下面用代码演示很简单 public static void main1(String[] args) {//yield关键字int ret1 5;switch(ret1) {case 1:System.out.println(1);break;case 2:System.out.println(2);break;case 3:System.out.println(3);break;default:System.out.println(4);break;}int ret2 5;switch(ret2) {//关于switch语句的简写case 1 - System.out.println(1);case 2 - System.out.println(2);case 3 - System.out.println(3);default - System.out.println(4);}//yield关键字用于代替-//case后面添加: yield后也加上:int ret3 5;switch(ret3){case 1: yield : System.out.println(1);case 2 : yield : System.out.println(2);case 3 : yield : System.out.println(3);default : yield : System.out.println(4);} 二 var关键字
var关键字根据变量的值判断并代表变量的类型这对于一些类型复杂的变量来说提高了代码的简洁性 public static void main(String[] args) {//var关键字//var关键用于通过变量值判断且代表变量的类型这样对于类型复杂的变量可以提高代码的简洁度//因此var关键字修饰的变量必须初始化不能为nullvar name zhangsan;var age 14;}注意事项
var不能声明成员变量var 不能修饰形参的类型var不能作为方法的返回值类型var修饰的变量值不能为null
三 密封类
sealed关键字与final关键字相同修饰的类也是密封类但是sealed修饰的类必须有子类而final修饰的类是最终类没有子类
关于密封类及其子类的思维导图
代码
sealed class Animal{String name ;int age;
}final class Dog extends Animal{}
non-sealed class petDog extends Animal{}
sealed class testDog extends Animal{}
non-sealed class test2 extends testDog{}四 空指针异常
在jdk8之前报指针异常时不会指出具体原因但是在jdk9之后报空指针异常时会指出具体的原因 public static void main(String[] args) {//空指针异常String name null;System.out.println(name.toLowerCase());}五接口中的私有方法
在jdk8 接口中可以实现默认方法在jdk9之后接口中可以实现私有方法因为接口中的私有方法不可能在外部被使用所以接口中的私有方法是为默认方法提供帮助的。
public interface Itest {
//默认方法default void func2(){func1();}
//私有方法private void func1(){System.out.println(hehe);}
}六instanceof关键字
在之前的博客中讲述到instanceof关键字用于判断左边的变量类型是否是右边的类型所转化。 在jdk9之后将这个过程简化了: Animal animal (Animal) new Dog();//简化前如果animal类型是Dog类型所转换则将animal类型转换成Dog类型if(animal instanceof Dog){Dog str (Dog) animal;}//简化后//如果animal类型是Dog类型转换自动创建一个Dog类型的变量str ,将animal类型转换成Dog类型赋给strif(animal instanceof Dog str){System.out.println(str);}在jdk17中还有一些其他的新增特性以后用到再进行阐述。