aspose file tools*
The moose likes Beginning Java and the fly likes What is the diff b/w string & String buffer? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the diff b/w string & String buffer?" Watch "What is the diff b/w string & String buffer?" New topic
Author

What is the diff b/w string & String buffer?

mara thamizhan
Ranch Hand

Joined: Jul 05, 2006
Posts: 74
Hi all,
I am a newbie to java programming.

I want to know the difference between using String & StringBuffer classes.

I read that String is fixed length character sequence,whereas StringBuffer is varied length character sequence!

plz explain!

cheers!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I read that String is fixed length character sequence,whereas StringBuffer is varied length character sequence!


Well, rather than saying "fixed length", we say immutable. Which means that once you've set its value, you cannot change it anymore.

String mystring = "a";
mystring = mystring + "b";

You think that you are using the same String variable, but in fact, JVM create a new one. You'll usually use StringBuffer to concatenate Strings. This avoids creating of new strings each time.


[My Blog]
All roads lead to JavaRanch
Amit Ghai
Ranch Hand

Joined: Mar 24, 2004
Posts: 35
This is an extract from the javadoc over String class. - Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.


Reasonable people adapt themselves to the world. Unreasonable people attempt to adapt the world to themselves. All progress, therefore, depends on unreasonable people.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
"You think that you are using the same String variable, but in fact, JVM create a new one."

replace variable with object


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
 
wood burning stoves
 
subject: What is the diff b/w string & String buffer?