Originally posted by Kanchan Narang: from K&B- Pg 500 Q 4 -> --- code --- String s="-"; Integer x=343; long L343=343L; Short s1=7; if(x.equals(L343)) s+=".e1"; if(s1 ==7) s+="=s";
------ First if return false Second if retruns true Can anyone explain why Thanks in advcance
x refers to an instance of the wrapper class Integer. L343 is a primitive long, that is autoboxed to an instance of a Long when you send it as the parameter to equals. The way a wrapper class is defined, if you call the equals method and send an instance of an object that is not that wrapper class, you will get a false.
In the second if statement, when you compare a primitive to a wrapper, the wrapper will be autounboxed.