This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help scjp5.0 question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "help scjp5.0 question" Watch "help scjp5.0 question" New topic
Author

help scjp5.0 question

nikunj thakur
Ranch Hand

Joined: Sep 23, 2007
Posts: 47
class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
} }

When // doStuff is reached, how many objects are eligible for GC?

0

1

2

Compilation fails.

It is not possible to know.

An exception is thrown at runtime.

in the above example the ans is C(2 objects are available for gc)
that i understood but the explanation given in the exam is this
C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.

can you explain
Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
The object referenced by c1 has a reference to another object of type Short.

at c1 = null, object referenced by c1 becomes eligible for gc, since this object has reference to another Short object, the second object automatically becomes eligible for gc.

That is why in total 2 objects are eligible for gc.


SCJP 5.0, SCWCD in progress
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14422
    
  23

Nikunj, please quote your sources.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jan Nowak
Greenhorn

Joined: Aug 12, 2007
Posts: 16
I think the answer should be: there is 1 object eligible for garbage collection. In this code the object Short story = 5; is not eligible for garbage collection because it's creation is optimized by the Java compiler. Remember that values between -128 and 127 are shared by Byte, Short, Character and Integer objects. You can easily check this concept and the answer to this question:


And for the above code the answer should be: one object is eligible for garbage collection.

However in this example there are 2 objects eligible for garbage collection:


[ November 17, 2007: Message edited by: Jan Nowak ]
Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
Its true that Java compiler optimizes the Wrapper objects and a given reference points to an existing object with same primitive value instead of creating new one, however, in this case,

Short story = 5;

is going to create a new Short object as their is no other Short object with same value that exists earlier.

Further, object pointed by "story" itself depends on object pointed by "c1".
Thus when "c1" becomes eligible for gc, object "story" automatically becomes eligible for gc.
Hence in total 2 objects are eligible for gc.
Robert Elbourn
Ranch Hand

Joined: Oct 15, 2007
Posts: 69
so why isn't c3 elligable for garbage collection ?

c3= c1.go(c2);

c1.go(c2) returns null

so isn't that the same as
c3 = null ?
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Robert Elbourn:
so why isn't c3 elligable for garbage collection ?


Because c3 was never assigned an object in the first place.


SCJP 5.0
P Ventura
Ranch Hand

Joined: Jan 24, 2007
Posts: 42
Originally posted by Robert Elbourn:
so why isn't c3 elligable for garbage collection ?

c3= c1.go(c2);

c1.go(c2) returns null

so isn't that the same as
c3 = null ?


I think c3 is eligible too. I wait a response from experts.


Objective: SCJP 1.5<br /><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html" target="_blank" rel="nofollow">API</a><br /> <blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre><font size="2"><br />Double n1 = Double.NaN; Double n2 = Double.NaN;<br /> n1.equals(n2) // true even though Double.NaN != Double.NaN<br />-0.0 == +0.0; // true<br />Double n1 = -0.0;Double n2 = +0.0;<br />n1.equals(n2) // false even though -0.0 == +0.0<br /></font></pre><hr></blockquote>
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637

First at line 1 a new CardBoard() object is created in heap and assigned to c1. Remember c1 is just a reference to the new object. During the process of this object creation as class CardBoard as a instance variable
Short story = 5; and since story is a reference to a wrapper object another new Short object is created and its assigned to story. So after line 1 executes two objects are created c1 referes to CardBoard object and its instance variable story refers to Short object.
So we now have 2 objects in heap.
After line 2 executes c2 refers to a new CardBoard object and this time its instance variable story is not instantiated but instead refers to the same object that was refered by c1.story. Remember Wrapper objects are immutable and to save memory JVM will use the same wrapper objects for
Boolean,
Byte,
Short & Integer [ -128 to 127]
Character
Since here 5 is well within the above range a new object is not created.
So after line 2 there will be three objects in heap.
c1 ---> CardBoard object 1
c1.story ---> Will refer to Short(5) object.
c2 --> CardBoard object 2
c2.story ---> Will refer to same Short(5) object as above.

Now at line 3
CardBoard c3 = c1.go(c2);
In the above call to go() c2 is passed and its assigned to cb [Pass by value]. This will assign cb to null and will not to anythign to c2.
The method go() returns null that is assigned to c3 reference . Remember @ line 3 we are simply creating a CardBoard reference but not instantiaing any object and this reference is now assigned to null.
After line3 the objects in heap are
c1 ---> CardBoard object 1
c1.story ---> Will refer to Short(5) object.
c2 --> CardBoard object 2

And the objects available for GC are
None

Remeber c2.story was never instantiated to a new Short object it was just a reference to c1.story as explained above.
After line 4 c1 is assigned to null and hence discarding any live references to Carboard object 1. So after line 4

objects in heap are
c1.story ---> Will refer to Short(5) object.
c2 --> CardBoard object 2

And the objects available for GC are
c1 --> CardBoard object 2

Even though there are no references to the Short(5) object created by c1.story it is still not eligible for GC as JVM will continue to keep this object in memory [Wrapper object poool , just like String object pool]

So the answer is 1 object are eligible for GC , the object that was referenced by c1.

Phew !!!
Thanks
Deepak


SCJP, SCWCD, SCBCD
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Jan Nowak:
I think the answer should be: there is 1 object eligible for garbage collection.


Ah, I missed this thread earlier somehow. Yes, Jan is absolutely right. Only one object is eligible for garbage collection here, because the second Cardboard instantiation will indeed assign story the same Short object as in the first Cardboard instantiation. The correct answer is that only one object is eligible for garbage collection.

The original poster did not cite a source for the problem, but I believe this comes from K&B, which unfortunately also incorrectly states that there are two eligible objects. To rectify this, the K&B errata list changes the short assignment statement to "Short story = 200;".
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
I have seen this question in Bert Bates book. I feel the answer should be 1 and not 2 objects.
Associated Short object is not available for GC because JVM will not make this available for GC inorder to save memory. If Short object was available for GC then a next instantiation of Short s = 5. would again have to create a new Short object then where is the question of saving memory. I think it works same as Strnig pool.
Kathy can you please suggest what happens here.
Thanks
Deepak
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Deepak Jain:
I have seen this question in Bert Bates book. I feel the answer should be 1 and not 2 objects.


Hi Deepak,

This problem has been discussed before. The answer given in K&B is incorrect, since they too forgot that the Short object will be reused. It should be one object, like you said. As I mentioned above, the official K&B errata list changes the statement to "Short story = 200;".
Robert Elbourn
Ranch Hand

Joined: Oct 15, 2007
Posts: 69
Originally posted by Kelvin Lim:


Because c3 was never assigned an object in the first place.


thanks!
Dara Sitaramayya
Greenhorn

Joined: Feb 26, 2008
Posts: 3
1 public class CardBoard {
2Short story = 5;
3
4CardBoard go(CardBoard cb) {
5cb = null;
6return cb;
7}
8
9public static void main(String[] args) {
10CardBoard c1 = new CardBoard();
11CardBoard c2 = new CardBoard();
12CardBoard c3 = c1.go(c2);
13c1 = null;
14//System.out.println("c1....."+c1.story);
15System.out.println("c2....."+c2.story);
16//System.out.println("c3....."+c3.story);
17}
18 }
In the above example,line 14 & line 16 are compilation errors.because both objects are null.That objects are eligible for GC.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help scjp5.0 question