clearly, the primitive values here are 2 and 2In order to save memory, two instances of the following wrapper objects (created through boxing), will always be == when their primitive values are the same:
• Boolean
• Byte
• Character from \u0000 to \u007f
• Short and Integer from -128 to 127
SCJP
Nikhil Pasupukuntla
SCJP 1.5 | OCPJWCD 1.5
nikhil pasupukuntla wrote:
The first output "false" makes sense because you have created two different integer objects using new operator and comparing the references which will be different.
nikhil pasupukuntla wrote:
The second print statment's output also makes sense since your comparing two integer primitives which are equal.
nikhil pasupukuntla wrote:
Now the third print statement's output is also correct because when you pass 2000 into the public void method( Integer i , Integer eye ) your wrapping that in a integer object but if the number you pass is between -128 to 127 then java would treat those wrapper as equal in order to save memory.You may ask y on the first print statement output is true this is becuase there you have manually created objects using new operator and in the third you passed it to the compile to wrap.
Nikhil Pasupukuntla
SCJP 1.5 | OCPJWCD 1.5
SCJP
Thapliyal Akshat wrote:
I have already mentioned in my first line that I am preparing for SCJP 1.6 exam.
SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Don't get me started about those stupid light bulbs. |