• 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/StringBuilder query

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the foll. lines of code-

StringBuffer sb = new StringBuffer ("abc");
sb.append ("def");
System.out.println ("sb = " + sb);

Is the string "def" created in the string constant pool, or is it just appended to the original object and a reference returned?

Thanks & Regards
Percy
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sb.append ("def");

is appending def to itself(sb)
We are not creating extra objects. So def is not created in the string constant pool.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Percy Dadabhoy:
...Is the string "def" created in the string constant pool...


Yes, as a String literal, I would expect "def" to be in the pool.

(See Strings Literally.)
 
Percy Dadabhoy
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Marc

Thanks for that link. I know about the string constant pool and the fact that the string literals that are used as part of a method's argument list are created in it.

It's just that where I found this piece of code (in the Sierra-Bates exam study guide), this fact is not mentioned explicitly when it comes to StringBuffer/StringBuilder though the book is very clear about it in the String section that precedes the former. So, since I wanted to be sure, I posted this topic.

Regards
Percy
 
We can walk to school together. And we can both read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic