This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object References & GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object References & GC " Watch "Object References & GC " New topic
Author

Object References & GC

Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Hello All

Integer i=100;

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.

Thanks
Srividhya
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

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 ]
Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Aditya

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.

Correct me if I am wrong.

Srividhya
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
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.)
Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Oh thanks for that information Bert
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

By the way, here are your answers:


1. Here s.i and s1.i refer to same Integer reference 100 in the cache.

True.


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.

That is correct!

Best regards,

- Aditya Jha
Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Thanks Aditya
Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
Posts: 113

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.
Thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Object References & GC