• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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
 
Whatever you say buddy! And I believe this tiny ad too:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic