• Post Reply Bookmark Topic Watch Topic
  • New Topic

String and StringBuffer  RSS feed

 
Ripan Karmakar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I just want to know the main diffeneces between String and StringBuffer.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This forum is for the discussion of threading and thread related issues.

Moving this to Java in General -- Beginner.

Henry
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the JavaDoc: String in Java 1.5 and StringBuffer in Java 1.5.

In general, the String is ideal for immutable data and StringBuffer (and StringBuilder) for mutable data.
[ October 27, 2006: Message edited by: Bauke Scholtz ]
 
paramu iyer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically after the creation of string its size can not be increased but String buffer is modifiable.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't change a String object, although sometimes it appears that you are doing just that. In this statement:

String str = "hello";

"hello" is called a string literal. A string literal is anything between double quotes. Behind the scenes, Java turns string literals into String objects. So, the assignment statement above has the effect of making str a reference to the String object "hello". Subsequently, you can make the variable str refer to other String objects, but the String object "hello" can never be altered--it will forever be "hello" and not anything else.

If you then do this:

String str = "hello";
str += " world";

A new String object will be created out of "hello" and " world", and str will subsequently refer to the new String object "hello world", while the String object that str used to refer to, namely "hello", will be discarded.

Behind the scenes, Java actually uses a StringBuffer object to add the two String objects "hello" and " world" together. After adding them together, Java then converts the resulting StringBuffer back to a String, and then assigns a reference to the String to the variable str. It turns out, it is more efficient to just use a StringBuffer yourself to avoid converting back and forth between Strings and StringBuffers which occurs when you add String objects together.

Now, on to StringBuffers. A StringBuffer is just an array of characters. So, when you do this:

StringBuffer str = "hello";

you end up creating an array like this:

Then, if you add a string literal to a StringBuffer, for instance like this:

str += " world";

the characters in the string literal are added to the StringBuffer's array of characters:



Therefore, when you add a string literal to a StringBuffer, it changes what's in the StringBuffer's character array, and that causes the StringBuffer to be altered.

...or something like that.
[ October 27, 2006: Message edited by: sven studde ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then, if you add a string literal to a StringBuffer, for instance like this:

str += " world";

the characters in the string literal are added to the StringBuffer's array of characters:


Note that the '+' operator is not ovferloaded in Java to include StringBuffers. A StringBuffer is mutated through its various append(), insert(), and delete() methods.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the '+' operator is not ovferloaded in Java to include StringBuffers.

oops.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!