Hi Srividhya!
The line of code
is
almost equivalent to
This means that, in a similar manner, the 'object' is created on heap and the reference 'i' is created on stack.
However, there is a fine line of difference.
For facilitating autoboxing in an efficient manner, JVM keeps some objects of wrapper classes in cache... how many of which class can be found in some book, but as far as I can recollect, its -128 to +127 for all numeric (non-floating types) wrapper classes and 0 to 255 for Character. Please refer to some book on this.
All this means that there won't be a new object getting 'created' by
if there exists an Integer object in cache representing value 100. You could say
will print 'true'. Whereas,
will print 'false'.
Moreover, if the value is greater than +128 (or whatever the limit is), then
will print 'false'. There will be 2 distinct objects created and they won't be cached by JVM.
Is this Integer reference 'i' created in the heap or in the stack and are they eligible for GC.
I know Integer i=new Integer(100); will create an object in the heap and they are eligible for GC.
As far as availability for GC is concerned, objects which are cached by JVM will not be eligible for GC. This would mean that
will not make any object available for GC at all. Whereas
will make the newly created Integer object available for GC.
Hope I make myself clear.
Best regards,
- Aditya Jha
[ May 14, 2008: Message edited by: Aditya Jha ]