aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bug test chapter 3, question 1? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bug test chapter 3, question 1?" Watch "Bug test chapter 3, question 1?" New topic
Author

Bug test chapter 3, question 1?

mr.cazurro greenlins
Greenhorn

Joined: Jan 17, 2011
Posts: 3
class CardBoard {
Short story = 200;
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 it reaches //do Stuff c1 and c3 are null, so 4 objects are eligible for GC and there is no test option for this...

am I lossing anything???
Malte Wannerskog
Ranch Hand

Joined: Jan 06, 2011
Posts: 92

mr.cazurro greenlins wrote:class CardBoard {
Short story = 200;
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 it reaches //do Stuff c1 and c3 are null, so 4 objects are eligible for GC and there is no test option for this...

am I lossing anything???


There is no bug. Remember that Java applies Pass-by-value. So in the method

cb is refering to the same object as c2, but by changing cb you are not changing the c2 reference.
A null reference is returned and assigned to c3. It does not change the c2 reference.

c2 is still referencing a CardBoard object. So the correct answer is that 2 objects are eligible for gc. The CardBoard object that c1 is refering to and its Short object.


OCPJP (310-065) - 88%
mr.cazurro greenlins
Greenhorn

Joined: Jan 17, 2011
Posts: 3
I know c2 is not null, but c3 is null, as c1.

so everything that applies for c1, applies for c3 so 4 objects are eliglible for garbage collector...

Malte Wannerskog
Ranch Hand

Joined: Jan 06, 2011
Posts: 92

mr.cazurro greenlins wrote:I know c2 is not null, but c3 is null, as c1.

so everything that applies for c1, applies for c3 so 4 objects are eliglible for garbage collector...



I dont know why you say that everything that applies for c1 applies for c3?

First of all only 2 CardBoard objects are created. c1 refers to one and c2 to the other.

The line
CardBoard c3 = c1.go(c2)
calls method go with reference c2. That means inside the method cb will refer to the same object as c2. But since Java applies pass-by-value(!) the only thing that happens with the line cb = null; is that cb no longer refer
to the same object as c2.

c2 will remain untouched after the methodcall and still refer to the same object as it did before you invoked go().
You can replace the line

with

And nothing would have changed.

c3 never refered to any object...
Lalaka Jeerasinghe
Greenhorn

Joined: Jun 04, 2010
Posts: 16


Here you are creating a variable c1 and c2 which refer to two CardBoard objects in memory.
Then you call c1's go method and pass the c2reference to it.
Now lets look at the go method!!



It takes the c2 as its parameter and set it to cb. and cb = null. Therefore c2 also null??....
Nooo!!
c2 and the cb in your go method both refer to the same Cardboard object in memory. Therefore, although the cb = null; the object cardboard is still referred by the c2.
The method returns a null reference and assigns it to c3.
Then you set c1 to null. So there is no longer a reference to the Cardboard object created by c1. Now that is an object eligible for garbage collection.
Short is a Wrapper class. So you have a Short Object which was residing in c1. That i another object eligible for GC
SO 2 OBJECTS ARE ELIGIBLE FOR GARBAGE COLLECTION.

TIP: take a sheet of paper and draw the objects in the memory and how they are referred. Don't forget objects created within the class(such as Short in this question.). You will approach the answer comfortably!!

Hope this clears your issue!!
Thanks,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bug test chapter 3, question 1?