Blog Archives
Comparing Integer in Java -autoboxing does not apply there
Hmm.. it came as most basic surprise for me, I wrote
this.questionId==question.getQuestionId()
where questionid is of type Integer (Object and not primitive).
Logically, it’s a reference comparison and should fail, but since its a wrapper for primitive type and a “special” scenario, my expectation was autoboxing will also apply here and will work properly, but no – it did not.
Correct way is either to use equals or just use intvalue
this.questionId.intValue()==question.getQuestionId().intValue()