对象判断空方法
Object s = Optional.ofNullable(null).orElse(new Integer(1));
boolean b = s instanceof String;
System.out.println("对象类型是否是String:"+b);
boolean c = s instanceof Object;
System.out.println("对象类型是否是Object:"+c);
boolean d = s instanceof Integer;
System.out.println("对象类型是否是Integer:"+d);
Optional.ofNullable(null).orElseThrow(()->{return new Exception("错误");});
打印结果
对象类型是否是String:false
对象类型是否是Object:true
对象类型是否是Integer:true
Exception in thread "main" java.lang.Exception: 错误
at Test.lambda$main$0(Test.java:31)
at java.util.Optional.orElseThrow(Optional.java:290)
at Test.main(Test.java:31)