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

# How many Objects are created ?

Greenhorn
Posts: 23
• Number of slices to send:
Optional 'thank-you' note:
StringBuffer s1 = new StringBuffer("abc");
StringBuffer s2 = s1;
StringBuffer s3 = new StringBuffer("abc");
How many objects are created ?

Ranch Hand
Posts: 45
• Number of slices to send:
Optional 'thank-you' note:
2

Thiru Mu
Greenhorn
Posts: 23
• Number of slices to send:
Optional 'thank-you' note:
cool..this is what i was expecting...
It was given wrong in the question i have refered..

Greenhorn
Posts: 13
• Number of slices to send:
Optional 'thank-you' note:
String �abc� is also an "Object".
answer should be 2+1 = 3
If I am wrong, please correct me.

Ranch Hand
Posts: 63
• Number of slices to send:
Optional 'thank-you' note:
first line creates an object in heap and also adds it to the string constant pool. The second line juss makes s2 point to s1. here s2 will point to the string "abc" in the constant pool and the third line will create a store abc in a new location in the heap.

So in total my guess is "abc" exists at three places 2 in the heap and the other in the string constant pool.

Now my doubt is only about counting the objects.. what should i count 2/3?? my guess is three.

Somebody please reply abt the count and correct me if any mistake !!

Thiru Mu
Greenhorn
Posts: 23
• Number of slices to send:
Optional 'thank-you' note:
hi

Objects can be created using only 'new'

so no confusion the object cunt here is 2....

correct me if i am wrong..

lowercase baba
Posts: 13089
67
• Number of slices to send:
Optional 'thank-you' note:
3.

Strings, and specifically string literals, are handled differently from other objects.

any time you have a new string literal (something in quotes that hasn't been seen before), it is created as a String object in the String Pool.

so, the fact that you have "abc" creates a String object in the pool. having the same literal still gives one object in the pool, so the second "abc" does not create a second String object.

Then, each call of "new StringBuffer()" will give you another object. so, you have the String "abc", and two StringBuffers, each of which has a value of "abc".

Ranch Hand
Posts: 274
• Number of slices to send:
Optional 'thank-you' note:
Thanks Fred for clearing doubt.

author
Posts: 9050
21
• Number of slices to send:
Optional 'thank-you' note:
because of the String constant pool Sun is phasing out questions that ask about how many "String" objects are created - you might get 1 question like this but you probably won't get any.

 My, my, aren't you a big fella. Here, have a tiny ad: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com