• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

number of objects created

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. StringBuffer s1 = new StringBuffer("abc");
2. StringBuffer s2 = s1;
3. StringBuffer s3 = new StringBuffer("abc");

How many objects are created ?

2 . Please correct me if i am wrong.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right. Only two objects are created. Because s1 and s2 are references pointing to the same memory.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, only two StringBuffer objects are created. The String object "abc" is the third.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s1= new String("abc");

2 objects are created. one in the heap and another "abc" in the string pool.

I have a doubt that if we assign null to s1 (s1=null then object created

in the string pool is available for garbage collection or not.

Please give the detailed explanation ....

Thanks in advance

Regards,
Krishna.
 
Raghusham Sankargal
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's some information to you hari, The object gc takes the object only when there are no references to it(internal or external). You cannot be sure when gc will be called and when it acts. This is what I think if I am not mistaken. Other ranchers please correct me if I am wrong.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hari Krishna:
String s1= new String("abc");

2 objects are created. one in the heap and another "abc" in the string pool.

I have a doubt that if we assign null to s1 (s1=null then object created

in the string pool is available for garbage collection or not.

Please give the detailed explanation ....

Thanks in advance

Regards,
Krishna.



Yes, 2 objects are eligible for garbage collection. But as you might know, when they will get collected, no body knows. It's on JVM. You can only request GC to run but can't force.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think answer should be 3
total 3 objects are created
1 "abc"
2 StringBuffer s1 = new StringBuffer("abc");
3 StringBuffer s3 = new StringBuffer("abc");

can anybody explain what is the correct answer , what are they ?
why people are counting "abc" and only 1 stringbuffer object ... what about the other stringbuffer object
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually this thread got hijacked from counting total number of objects created to total number of objects GC'd

Totally there are 3 objects ( 2 String buffers ) & a String in the StringPool.When S1 is set to null then only the StringBuffer pointed by s1 is eligible for GC and not the string in the string pool
[ December 15, 2005: Message edited by: Srinivasa Raghavan ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic