• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

How many objects?

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,

Please consider he following code.Its a question from examsguide.com.



Ans : d

But i think the answer is 2. ie c. PLease somebody correct me if i m wrong.


I think the answer for this question should be 1 object.ie The object referred to by c2.Please correct me if i m wrong.


Thanks in advance.

Uttara Rishi.

[ March 10, 2008: Message edited by: Uttara Rishi ]
[ March 10, 2008: Message edited by: Uttara Rishi ]
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In code block 1, 3 is the correct answer because one object which you might have missed is the string literal "abc" object which will get created in the string pool.

In second, 1 seems to be correct to me as well because the method m1() wil always be returning a new object which will not have any sort of link with the object passed in the arguements.

Regards.
 
Uttara Rishi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rohit,



I still dont get it.
I think s1 and s2 both refer to the same object. Dont they...?
Please correct me if i m wrong.


Uttara Rishi.
 
Rohit Garg
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right on that but the three objects i was talking about are :

1. String -> "abc"
2. StringBuffer -> s1
3. StringBuffer -> s3

Hope this helps.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Can any one please put more light on the Question II that Uttara Rishi has asked

Because as I understand there are total 3 objects created c1, c2, c3
Then c2 got the same reference as c3 - So the earlier object to which c2 was pointing is free for Garbage collection.

So only 1 object is un referenced, am I missing something, can anyone explain me ?
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah..I didn't get it either..

I thought obj1 is pointing to the same object as c1(when invoking m1())
Later a new local object is being created and being assigned to obj1.
Does that mean its reference has changed to the new object or is it still referring to c1 (as it was).
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think C1 and C3 both are eligible for garbage collection as new object of C is created and reference is assigned to ob1.

Thanks
Sandip

[ March 12, 2008: Message edited by: Sandip Kaviman ]
[ March 12, 2008: Message edited by: Sandip Kaviman ]
 
Rohit Garg
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The object which obj variable references has no link with the one that c1 used to refer to after the execution of the line :

obj = new C();

Thus, the answer would surely be 1 which is the object reffered to by c2.

Regards.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think for
Question I:

1. StringBuffer s1 = new StringBuffer("abc");
2. StringBuffer s2 = s1;
3. StringBuffer s3 = new StringBuffer("abc");

answer is -> c: 2
because here reference variables s2 and s1 are referring to the same object. while ref variable s3 referring to another object.


Question II:

class C
{
public static void main(String a[])
{
C c1=new C();
C c2=m1(c1);
C c3=new C();
c2=c3; //6
anothermethod();
}
static C m1(C ob1){
ob1 =new C();
return ob1;
}
}

i think -> Ans: 1.
here, the c2 was referring to the another object. but at line 6 c2 and c3 are referring to the same object. so the reference to the previous object is broken. so this object is now referring by no reference var. while c is already referring to one object.
so only one object is available for GC.
thank you.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic