• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How many Objects are created ?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer s1 = new StringBuffer("abc");
StringBuffer s2 = s1;
StringBuffer s3 = new StringBuffer("abc");
How many objects are created ?
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2
 
Thiru Mu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool..this is what i was expecting...
It was given wrong in the question i have refered..
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String �abc� is also an "Object".
answer should be 2+1 = 3
If I am wrong, please correct me.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Fred for clearing doubt.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic