Forums Register Login

Sun's mock question 4.

+Pie Number of slices to send: Send
Can someone kindly explain why the answer should be option 3, since only a copy of the local variable x was passed into y, and not the variable itself, I thought the answer should have been never, or is there something else I should learn.

4. Given:
public class X {
public void m(Object x) {
x = new Integer(99);
Integer y = (Integer)x;
y = null;
System.out.println("x is" + x);
}
}
When is the Integer object, created in line 3, eligible for garbage collection?
never
just after line 4
just after line 5
just after line 6 (that is, as the method returns)
when the calling method sets the argument it passed into this method to null
+Pie Number of slices to send: Send
Hi sola,
Look at the question once more.
"When is the Integer object, created in line 3, eligible for garbage collection? "
Itz not asking about the object which is pointed by "x"
[ "x" is infact the copy of the orginal reference ] before the method is called.
-------------------------------------
Lets modify the code to make it more clear.
class A {
public static void main(String args[]) {
X b = new X();
String n = "aryan";

// The copy of the reference "n" is passed to the method m().
// The orginal "n" points to the string "aryan" . rite ?

b.m(n);
}
}
public class X {
public void m(Object x) {

// remember "x" is the copy of "n"

// Now "x" will point to the new object, no more to the orginal String object it was pointing.

x = new Integer(99);

// now "y" also point to the same object pointed by "x"
// [ an instance of Integer ]


Integer y = (Integer)x;

// now the above object created got only one reference ie "x"

y = null;
System.out.println("x is" + x);

// "x" is going to die, now , since its life period ends after the
// method call. V r talking about the "x", which is a copy of
// "n".
// So the "x" dies, Integer object is orphaned, and Garbage collector
// gets a an orphan to sweep, and its happy. [ the String object is
// still living happly some where in the memory, coz, it still has
// a reference, "n".

// So question was abt the Integer object not abt the String object.
// now u know the answer .. rite ?
}
}

[This message has been edited by Jon Aryan (edited October 08, 2000).]
+Pie Number of slices to send: Send
Then the answer should be 4,
since after line 5 the Integer object still has a pointer x to it.
Only after Line 6,x dies and Integer object is eligible for garbage collection.
+Pie Number of slices to send: Send
As I know, here in line no. 4
Integer y = (Integer)x;
var y refers to the reference of x, not the copy. So if you change y to null, x will also be null.
So the answer is 'Just after line 5'
Thanks
Santosh Jaiswal
+Pie Number of slices to send: Send
Santosh,
Unfortunatly you have to break this down:
line 2: public void m(Object x) {
This line declares a method that takes an argument which is
an Object reference.
line 3: x = new Integer(99);
Here a new Integer Object is created (the "new Integer(99)"
part), then the Object reference is changed to refer to the
newly created Integer object (the "x = " part).
line 4: Integer y = (Integer)x;
Here the Object Reference x is "cast" to an Integer reference (the "(Integer)x" part, this allows this part to of the line to be treaded like an Integer reference). Next a new Integer reference is created, and it is set to refer to the same object as x (the "Integer y =" part).

Remember, references are like signs pointing toward an city.
You can have as many signs pointing towards Chicago as you want, but if one falls over so it points towards Latvia all the rest still point towards Chicago.(sorry, bad analogy)
I hope this helps,
Jason

+Pie Number of slices to send: Send
thanks to you all guys, i think I understand the question now. but i sure hope i do not get a variation of this in the real thing.
+Pie Number of slices to send: Send
So finally ...option 4 is correct.
Am I wrong !
Manish
+Pie Number of slices to send: Send
Jason,
I don't get your point, Since y and x both point to the same reference, and if the value at that reference change, both will change.
Where am I wrong? can anybody explain it more clearly
Thanks
+Pie Number of slices to send: Send
 

Originally posted by Santosh Jaiswal:
Jason,
I don't get your point, Since y and x both point to the same reference, and if the value at that reference change, both will change.
Where am I wrong? can anybody explain it more clearly
Thanks



Let's see if I can explain.
First you have to understand how the references work. A reference contains two pieces.. an address of itself and value of the reference (I guess you could say that a reference is an object too). Here is how it would look:
<PRE>
reference
___________
| |
| address |
|_________|
| |
| value |
|_________|
</PRE>
Let's assume you create a reference type of int as below:

  1. Integer y;

  2. y = new Integer(1);


  3. This is what happens in the memory...

    1. Runtime creates a reference object of type Integer in the memory, let's say the address is 100 in the memory and the value is not yet intialized so it's not pointing to anything, not even a null.
      <PRE>
      y
      _______________
      | |
      | address = 100 |
      |_______________|
      | |
      | value |
      |_______________|
      </PRE>

    2. In second line the runtime is doing two things. First it creates an object of Type Integer, let's say the address of Integer is 200 in the memory.
      <PRE>
      Integer
      _______________
      | |
      | address = 200 |
      |_______________|
      | |
      | value = 1 |
      |_______________|
      </PRE>
      Second it initializes the reference y. In other words it sets the value of y to address of Integer, which is 200. So your y reference looks like this:
      <PRE>
      y
      _______________
      | |
      | address = 100 |
      |_______________|
      | |
      | value = 200 |
      |_______________|
      </PRE>


    3. Now if you create a new reference type of Integer, let's say x and give it the same value as y as in..
      <pre>
      Integer x = y;
      </pre>
      In essence what you are doing is initializing the value of x reference to the value of y reference.
      <pre>
      x
      _______________
      | |
      | address = 300 |
      |_______________|
      | |
      | value = 200 |
      |_______________|
      Now if you say
      y = null;
      you are not actually changing the object at memory location 200, rather you are changing
      the value of reference y to null
      y
      _______________
      | |
      | address = 100 |
      |_______________|
      | |
      | value = null |
      |_______________|

      Now you still have x pointing to Integer at address 200, so it is not yet available for garbage collection.
      BTW, the answer in mock exam is wrong. It should be option 4 and you can verify it by running this:
      It should print out 99 and not null, hence option 3 is incorrect.
      public class X {
      public static void main(String argv[]){
      X x1 = new X();
      x1.m(x1);
      }
      public void m(Object x) {
      x = new Integer(99);
      Integer y = (Integer)x;
      y = null;
      System.out.println("x is" + x);
      }
      }
      </PRE>
+Pie Number of slices to send: Send
Manish, to ease your mind, I think it's option 4, too...after line 6 has finished executing.
Santosh, I am not sure if I'm just reading your comment wrong or not, but this line: y= null; will not change the value at that reference, rather it just changes the actual reference of y. Once, y was referencing the same thing as x, but now y is referencing null. x is still referencing that same object, but y is not.

I don't know if you go boating much or not, but here's a little analogy for you. A brand new sailboat has been launched and is tied to the wharf with one rope (x = new Integer(99)).
Shortly after they tie another rope to the same boat (y = (Integer)x).
Okay, so then the rope person got bored and decided he/she didn't want that second rope on there anymore (??) so they untied the rope and let it dangle in the water (y=null).
That first rope is still there and so is the boat. Just because the second rope was undone, it doesn't mean that both ropes are gone.
Because the rental of wharf space was only for a certain amount of time (the length of the method) when that time is up, the rope person unties that rope, too, letting the boat drift away into wherever it is that boats go when they are no longer tied up...Davey Jones Locker, I guess.
Anyway, I hope that helps and if not, I hope you enjoyed reading the story of a boat <cool>
Paul

+Pie Number of slices to send: Send
I am just rewriting the same code adding static main here.
public class Q {
public void m(Object x) {
x = new Integer(99);
Integer y = (Integer)x;
y = null;
System.out.println("y is " + y);
System.out.println("x is " + x);
}
public static void main(String args[])
{
new Q().m(new Integer(10));

}
}
if you run this program you get result "y is null& x is 99";
Still x is pointing to object 99.
It only dies on method returns.
I feel answer is option 4(Method returns);
+Pie Number of slices to send: Send
dear all
I believe the answer is just before line 6. why ?--because

very simple
the object will only be gc'ed when

1. all its direct or indirect references are set to null
2. OR it is outof scope of a method provided that it should not have any backup reference some where else in the program
So in our puzzle it is in out of scope just after line 6 .
OK!
IF MY EXPLANATION IS WRONG PLEASE RECTIFY .
---SHIVARAM
+Pie Number of slices to send: Send
Bhupinder, thanks for a detailed explaination. Really it will clear anyone's doubt. Also thanks to Paul and other guys.
Everyone is a villain in someone else's story. Especially this devious tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 845 times.
Similar Threads
Sun Sample queston #3
Sun's Sample Question..
answer to sun sample question
Qn from Sun's samples
About Garbage Collection
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:22:00.