In case of go() method primitive int is auto-boxed to wrappered Integer. It will create new objects, for new objects == will return false as their memory location would be different, whereas if you try .equals() method it will return true
Regards,<br />BulletProof Monk.<br /> <br />"Nthing the Mind of man can Conceive<br />and Believe,It can achieve"
i1=2, i2=2: For ASCII value (int -128 to 127), it will compare it as an int value (so gives true). i1=4000, i2=4000: For other int value, it will pass it as an Integer object as shown in the code (So gives false).
Auto boxing for ASCII value for int works different here.
Look code of Integer.valueOf() method in Integer class. You will get your answer.
If you call with i1=2, i2=2, then JVM will not going to create new Integer object. Why?
Here Integer.valueOf(2) method is get called not new Integer(2); And this method has already created a cache for Integer object of 256 size that will contain all Integer object starting from -128 to 127. From -128 to 127, no new Integer objects are created, as they are already created and cached.