This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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

Why still we are using "String" ?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Now we have advance version of String i.e. StringBuffer and StringBuilder,
Why still we are using "String" ?
Can anyone tell me why?
 
Sheriff
Posts: 67592
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuilder and StringBuffer are not "advanced" versions of String. They are utility classes used to build strings dynamically. They should not be used in place of String when the string value does not need to be built or changed internally at run-time.
 
Bear Bibeault
Sheriff
Posts: 67592
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exercise: write code that builds a StringBuffer containing your name without using instances of String or string literals.
 
Sheriff
Posts: 3036
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On a similar note, why are we still using StringBuffer when we have StringBuilder? StringBuilder is faster because it's not synchronized, and I've never seen a use of StringBuffer that actually needed the synchronization it provides. I've lived a pretty sheltered life though.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greg: StringBuffer has been superseded by StringBuilder since Java 5, exactly because of the reason you mentioned: StringBuffer had unnecessary synchronization.

Since Oracle (and before them, Sun) always has to be very careful about backward compatibility, they couldn't just take the synchronization out of StringBuffer - that would break programs that depend on it (even though almost nobody needs and uses it). They even didn't deprecate StringBuffer, probably because they were afraid to annoy too many people who were using StringBuffer.

In new code, always use StringBuilder instead of StringBuffer.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I most frequently just use String concatenation instead of StringBuilder. The code is more readable in my opinion and the compiler optimizes to use StringBuilder under the hood anyway, in most cases, not all. On recent (>=6U23) server JVM implementations escape analysis should be supported and performed by default, so lock elision should eliminate StringBuffer's synchronization overhead where applicable. Might be useful to know if you're stuck with legacy code that uses StringBuffer all over the place, no need to go maniacally replacing it with StringBuilder.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I most frequently just use String concatenation instead of StringBuilder.


It really depends on the size of the String and number of concatenations you are performing but if you are doing many concatenations on very large strings explicitly using a StringBuilder can dramatically improve performance. I've tested many compilers new and old and all the ones I've tested, for string concatenation in a loop, create a new StringBuilder and String Object for each iteration of the loop.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it does indeed depend on the situation, which may not always be appearant. So then it might be preferable to just always use StringBuilder, especially when working in a team, for the sake of consistency. I woudn't though, as in a lot of cases that would mean sacrificing readability. Also, who knows, maybe String concatenation will be subject to further optimization in the future like with the StringBuilder replacement by the current compiler. I guess the ever annoying "it depends" applies here as well.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sanket Akre wrote:Why still we are using "String" ?


Quite apart from everything that Bear said: Strings are Thread-safe. Therefore, you can happily share them around any multi-tasking app you've written and KNOW that you'll never have any problems. Try that with StringBuilder or (in some situations) StringBuffer.

Winston
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jelle Klap wrote:Yes it does indeed depend on the situation, which may not always be appearant...


I would imagine most String concatenations in loops aren't long enough strings or a high enough number of iterations to have any noticeable effect on performance and the remaining few cases would be easily picked up by a profiler.

Jelle Klap wrote: I woudn't though, as in a lot of cases that would mean sacrificing readability


Personally I don't find using StringBuilder less readable than String concatenation so I always use StringBuilder when concatenating in a loop but if you do find it less readable then I agree in most cases it's not worth doing. And like you said when working in a team it's important to take a consistent approach.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic