Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

in which scenario , we can use string and stringbuilder.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
welcome to javaranh


please some one explain in which scenario we can use string and stringbuilder.






Thanks and regards
sivakondalu
 
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
A StringBuilder is usually used as an intermediary to build a single String object out of lots of other Strings, primitives or Objects with a sensible toString() implementation. Depending on the situation it can be a more efficient alternative to the basic String concatentation using the + operator. Remeber that Strings are immutable, so every concatentation will result in the creation of a new String (*). However, you should not view or use it as a drop in replacement for basic String concatenation! In spite of the fluent API of the StringBuilder class, which allows you to chain append() calls, it is a far less readable alternative, in my opinion. Furthermore, the compiler is in many cases smart enough to replace basic String concatenation with a StringBuilder under the hood.

* In practice that's not exactly how it will work in most cases, due to the compiler optimization (read on).
 
sivakondalu dokku
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you jelle klap
 
Marshal
Posts: 75648
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joshua Bloch in Effective Java™ warns against multiple String catenations. But it is all right to use as many + signs on Strings as you like, provided they are all in the same statement, because the optimisation Jelle told you about will sort that problem out.
 
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

sivakondalu dokku wrote:thank you jelle klap


The only thing I would add to Jelle's good advice is: generally, you shouldn't return StringBuilders from a public method, because someone might then go and change it on you.

Winston
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic