例子:
public static class Student {
private String name;
private Integer age;
private Integer isAdult;
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public Integer getAge() {
return this.age;
}
public void setIsAdult(Integer isAdult) {
this.isAdult = isAdult;
}
}
public static void main(String args[]) {
Student student = new Student("张三", null);
if (student.getAge() >= 18) {
student.setIsAdult(1);
}
}
判断语句那里就会报错,因为Integer是一个包装类,在与基本数据类型作比较时会转换成对应的数据类型进行比较,转换的时候就会报空指针异常,应该吧:-D