This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
this is beacause from 1 to 127 values are stored somewhere in cash (you can think it like as string constant pool.) So once if a Integer object ranging from 1 to 127 is careted ,then when another object of same value needs to be created it will give the reference of the old object.
Indeed, class Integer has a caching mechanism, but this only works when you're getting Integer objects by calling Integer.valueOf(...), or by autoboxing a primitive int into an Integer (by doing this, Integer.valueOf(...) is implicitly called).
If you create new Integer objects explicitly, then two new Integer objects are created.
If you would do this:
1. Integer i1 = 127; 2. Integer i2 = 127;
Then i1 and i2 would both refer to the same Integer object.
I haven't really done a deeper study on Wrappers except for the ones discussed in the KB book. I have learned that wrappers are immutable though. Is the concept of immutability of Wrappers the same concept with that of Strings?