如何评估以下表达式?Java中的自动装箱问题
Student类:
public class Student
{
private Integer id;
// few fields here
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id=id;
}
//setters and getters
}
而且在一些方法:
{
int studentId;
// few lines here
if(studentId==student.getId()) // **1. what about auto-unboxing here? Would it compare correctly? I am not sure.**
{
//some operation here
}
}
不要使用包装类,除非你绝对必须。 – mre
是的。你做了什么会工作。我不确定你问了其他什么? – Guillaume
+1不使用包装类:不好的潜在副作用包括不需要的(和隐藏的)NullPointerException抛出您的代码 – Guillaume