• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

what is the difference between String and StringBuffer?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
many people say StringBuffer is more efficient ,that's why?

String str1 = "Hello ";
String str2 = "EveryBody"
str1 +=str2; //when do this ,my friends say will new StringBuffer(),and then call append(),then call toString() to return a String .Is this true?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there are lot of concatenations- Use StringBuffer. For one or two concatenations I dont think StringBuffer is required. For every concatenation creates one new string in the String pool.
May be you can consider StringBuilder if are not concerned about Thread safe operations.
 
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I compile that, then decompile that using JAD I get this:
You'll see two things:
1) the variable names are changed. That's because local variables are not stored in the compiled byte code.
2) the += is indeed replaced by the creation of a new empty StringBuilder, to which then both Strings are appended, after which it's toString() method is called.

Now about whether or not using a StringBuilder is more efficient, it depends:
1) if you only append two constants or literals, using + is more efficient. For instance, if I write "Hello " + "Everybody", the compiler will use String literal "Hello Everybody". The same holds for final variables for which the value can already be known by the compiler.


2) if you append only two Strings, String's concat method is actually preferred, because you don't create the intermediate StringBuilder but immediately create a String with the contents of the two source Strings.


3) if you only have one statement that creates the String, using + is perfectly fine. The compiler may change the code to use something even better in the future.


4) if you add to the Strings conditionally (with an if) or in a loop, an explicit StringBuilder is usually the best choice.

One final word on efficiency: if the StringBuilder's capacity needs to be increased a lot, it may loose a lot of its efficiency. If possible, specify the expected total capacity when creating it: new StringBuilder(expectedCapacity).
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob for an informative explanation.
 
Marshal
Posts: 74047
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch zhiqiang zhang
 
Rob Spoor
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

By the way, you don't see concat a lot. That's because using + is still easier to write, and it also automatically handles type conversion from anything to String. I can't remember ever having used concat.
 
zhiqiang zhang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank all of you ,i learn a lot ,thank for Rob Spoor's patience
 
reply
    Bookmark Topic Watch Topic
  • New Topic