• Post Reply Bookmark Topic Watch Topic
  • New Topic

Diff between String & String Buffer  RSS feed

 
Kuladip Yadav
Ranch Hand
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi Java Ranch Users,
Will you please explain me.

What is diffrence between String & String Buffer ?
and Which is good for with situation ?
 
Minal Silimkar-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear only4java, please see the naming policies, here.

Answer for your question, "What is diffrence between String & StringBuffer?"

String is immuttable and StringBuffer is mutable.
[ July 30, 2008: Message edited by: minal silimkar ]
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to go through the API documentation for StringBuilder (don't use StringBuffer for most beginner's applications) and String.
[ July 31, 2008: Message edited by: Campbell Ritchie ]
 
Kuladip Yadav
Ranch Hand
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by minal silimkar:
Dear only4java, please see the naming policies, here.

Answer for your question, "What is diffrence between String & StringBuffer?"

String is immuttable and StringBuffer is mutable.

[ July 30, 2008: Message edited by: minal silimkar ]


Hi,
Thanks for suggestion.
 
Kuladip Yadav
Ranch Hand
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by minal silimkar:
Dear only4java, please see the naming policies, here.

Answer for your question, "What is diffrence between String & StringBuffer?"

String is immuttable and StringBuffer is mutable.

[ July 30, 2008: Message edited by: minal silimkar ]


Hi,
Thanks for suggestion.
Now I have used my name.


Thanks & regards
 
Kuladip Yadav
Ranch Hand
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Minal Silimkar is quite correct. The naming policy is enforced.

You need to go through the API documentation for StringBuilder (don't use StringBuffer for most beginner's applications) and String.


Campbell

performance point of view with is best in String and String Buffer ?
Thanks & Regards
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need many alterations to the String, use StringBuilder instead.

Consider the following code for formatting an array "o":

Now what the compiler actually makes of it is the following (as seen when I decompiled it):

As you can see a new StringBuilder is created for each append operation.

Now we can replace that with the following, with only one single StringBuilder object:
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kuldeep Yadav:


Campbell

performance point of view with is best in String and String Buffer ?
Thanks & Regards
Neither is "best." It is a bit like asking whether a motorbike or a speedboat is faster. Very few motorbikes are faster on water and very few speedboats on the road. As Rob has told you, StringBuilder is used for text you want to change, and String for text you don't want to change.

StringBuilder usually gives faster performance than StringBuffer.

Repeatedly using append() on a StringBuilder usually gives much faster performance than repeatedly using the + operator on a String, but if you use the + operator on Strings several times in the same statement there is an optimisation which uses StringBuilders so the performance is the same in both instances.
 
vikram veera
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) String objects are constants and immutable whereas StringBuffer objects are not.
b) String class supports constant strings whereas StringBuffer class supports growable and modifiable strings.

Thanks
Vikram
For more details see the below URL
developerparadise.com
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!