• 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

StringBuffer?

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

I found this in one of the mock exam on the phil/simon book. Any anybody please explain the reasoning.



Answer given: 3

Should be not be 2. Since s2 is only a reference to the object created at s1?

Thanks
-Kits
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For passing the SCJP: the first object is the string "abc", the second object is the StringBuffer object constructed from "abc" and referenced by s1, and the third object is the StringBuffer object referenced by s3. This third object is constructed from the same "abc" as used to construct the first StringBuffer object.

As you correctly said, s2 is only a reference to the same object that s1 references, no new object is created.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yups,

Three objects are created by this code, one string literal("abc") is created in the string pool and the other two string buffer objects are created on heap.
 
Kitty Dayal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry, Vivek

Will this one also create three objects?



One in the pool and two on heap.

Also I am not sure as to what kind of objects use string pool. (I thought only String class uses it).

Thanx,
-Kits
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. this code will also create 3 objects. one on the string pool and two on the heap
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic