• 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
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

GC ?

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
here is a question from K&B, clarify!!
public class X
{
public static void main(String[] args)
{
X x = new X();
X x2 = m1(x);
X x4 = new X();
X2 = X4;
doComplexStuff();
}
static X m1(X mx)
{
mx = new X();
return mx;
}
}
After line 6 runs, how many objects are eligible for garbage collexn. ?
A. 0;B. 1;C. 2;D. 3;E. 4
K&B's ANSWER: B, REASON:: since JAVA IS PASS BY VALUE, so reference variable x is not affected by the m1() mthd.
When passing objects in JAVA, Is it not pass by reference ?....
Want to get my basics strong enuf ~ Shalini
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Taurean, and welcome to the ranch.
The answer given by K&B is correct, there is only one object eligible for garbage collection.

The reference that is created on line 2 (x2), looses its reference at line 4.
[ January 01, 2004: Message edited by: Vicken Karaoghlanian ]
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Taurean. The K&B book explains this topic (Passing variables into methods) well on page 183.
 
Ashok Paulraj
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Vicken & Dennis --- I also read an article from Javaranch CAMPFIRE story, it cleared out everything by what does K&B refers by "JAVA is pass by value in everything"
more than the goal of passing SCJP 1.4, I am getting myself appreciated to learn the LANGUAGE well...hence, I don't want to hurry up towards my SCJP1.4 exam )
~ Shalini
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
I am new here,
I want to put it like this ,

if you pass a ref to a method then use that ref and manipulate the object, if you manipulate the ref itself then is has no effect out side the method.

Right?
Rafay.
something related to GC : The Story Of Resurrection
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abdul Rafay Mansoor:
hello,
I am new here,
I want to put it like this ,

if you pass a ref to a method then use that ref and manipulate the object, if you manipulate the ref itself then is has no effect out side the method.

Right?
Rafay.
something related to GC : The Story Of Resurrection


Hi Abdul Rafay, and welcome to the ranch.
When you call a method and send any reference to it as a parameter, then only a copy of that reference is sent, not the reference itself.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic