This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

answer to sun sample question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what will be the answer to the following question?
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
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer is
just after line 6 (that is, as the method returns)
Thanks.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'abhishek',
PROPER NAMES ARE NOW REQUIRED!! and your name doesnot comply with the official naming policy.
Please read this post for more details and register again with a valid name.
Ajith
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Since Objects are passed by reference and not by value, even when the method returns the Object created in line 3 will still be reffered by the Object x passed on when calling the method. Till the calling reference is not set back to null, I think this object can not be GC.
Thanks

Originally posted by abhishek:
what will be the answer to the following question?
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


 
Rajiv Ranjan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I totally agree with Saumil, but with the exam point of view I think the most suitable answer is :
just after line 6 (that is, as the method returns)
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saumil,
u are right but...it's wrong if we think that
reference x that points to object new Integer(99) vanishes
when the method return(the scope of x is the scope of m) ???
So,the answer it's :just after line 6 (that is, as the method returns)
rgds,
Cristi
------------------
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,
If you didn't know the one and only parameter passing mechanism Java uses is pass by value. In case of primitives the primitive value is passed by value, in case of references - refernce to an object is passed by value (not the object of course, so this creates a misconception that Java passes parameters by reference in case of objects). You were right, Integer(99) is a candidate for GC after line 6. To add, assignments to x within this method do not affect the reference that x was copied from, so if I call m like this:
<pre>
Object y = new X();
y.m(y);
</pre>
y will point to object of type X all the time, even after line 3 in m is run.
Vlad.
 
Cristi Tudose
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vlad,
do u think,your piece of code is a valid one???
I don't,but i get what you want to say...
Object y = new X();
y.m(y);
rgds,Cristi
 
abhishek
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody for the answers.
abhishek p.
 
Vlad G
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I made a small mistake (forgot about type cast), but in general my fragment of code is syntactically correct. The following compiles and runs OK:
<pre>
public class Dumb
{
Dumb()
{
Object y = new X();
((X) y).m(y);
}
public static void main(String[] args)
{
new Dumb();
}
class X
{
public void m (Object x)
{
System.out.println("In m");
}
}
}
</pre>
-VG.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic