返回NULL作为三元运算符允许的int,但不允许if语句
public class Main { private int temp() { return true ? null : 0; // No compiler error - the compiler allows a return value of null // in a method signature that returns an int. } private int same() { if (true) { return null; // The same is not possible with if, // and causes a compile-time error - incompatible types. } else { return 0; } } public static void main(String[] args) { Main m = new Main(); System.out.println(m.temp()); System.out.println(m.same()); }}
temp()
int
null
return true ? null : 0;
NullPointerException
.
if
same()
九州编程
阿波罗的战车
Helenr
相关分类