private static Integer getInteger() {
return null;
}
public static void main(String[] args) {
boolean condition = false;
// error: null pointer exception
Integer i = condition ? 1 : getInteger();
if (Objects.equals(i, 1)) {
System.out.println("i == 1");
} else {
System.out.println("i == null");
}
}
为什么是 getInteger() 返回值退化成 int 而不是 1 提升为 Integer ?