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.
Thanks for your detailed reply. I have some more doubt to be clarified.
1. Here s.i and s1.i refer to same Integer reference 100 in the cache. 2. Suppose if I make s=null, only one object will be eligible for GC which is first GC object 's' and not 'i' since it is referenced by s1.i. 3. If I change the value of i from 100 to Integer i=128 then two objects are eligible for GC which is 's' and the Integer reference s.i. Since s.i and s1.i refer to two different Integer references.
This is an interesting and tricky topic. The good news is that, for the SCJP exam, you won't have to deal with how the GC interacts with objects that can go into pools or caches, etc. So, GC questions won't use wrapper type objects or Strings.
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
1. Here s.i and s1.i refer to same Integer reference 100 in the cache.
2. Suppose if I make s=null, only one object will be eligible for GC which is first GC object 's' and not 'i' since it is referenced by s1.i.
You are correct. Also, I would like to add that even if you nullify 's' as well as 's1', the only objects available for garbage-collection would be the two objects of class GC. The object of class Integer will be referred by internal cache of JVM, and will never be available for gc.
3. If I change the value of i from 100 to Integer i=128 then two objects are eligible for GC which is 's' and the Integer reference s.i. Since s.i and s1.i refer to two different Integer references.
I want some more Information about Pool and caches for the purpose of knowing it in detailed because GC is my most favorite topic, I never miss it. I want to know How and When wrapper classes objects and String objects are eligible for GC. I want to know with a very fine Information about GC which i can remember throughout my lifetime and only you java ranch people can help me and others.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com