Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

number of objects created

 
Naresh Gunda
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Raghusham Sankargal
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. Only two objects are created. Because s1 and s2 are references pointing to the same memory.
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, only two StringBuffer objects are created. The String object "abc" is the third.
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
raghava yelamkonda
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic