aspose file tools*
The moose likes Beginning Java and the fly likes Diff between string and 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 "Diff between string and string buffer" Watch "Diff between string and string buffer" New topic
Author

Diff between string and string buffer

Jethram Boda
Greenhorn

Joined: Sep 19, 2007
Posts: 1
Diff between string and string buffer
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
Welcome to JavaRanch.

The javadocs for String and StringBuffer do not look very much alike, indicating that they are quite different. In what way do you perceive them as being alike?


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Biggest difference:

String is immutable; every method that returns a string returns a new object.
StringBuffer (and StringBuilder in Java5 and up) is mutable; (nearly) every method changes the object itself, and the object itself is returned.


However, as Ulf said, they are very unsimilar. The better question would be: what are the similarities between String and StringBuffer?

The answer to that would be, they both implement CharSequence, so you can get access to every character of the object you want.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Jethram Boda:
Diff between string and string buffer


I'm very surprised you got an answer to this question. You've posted in the wrong forum (this is not an Advanced Java question), you've used non-words ("diff"), your post is not a sentence (or a question) and you haven't even typed the Java class names properly.

You very welcome to the Ranch, but I recommend you should read HowToAskQuestionsOnJavaRanch to get the most from it.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375

Well... I tried "diff String.java StringBuffer.java" on my UNIX box, but the result is too long to paste here
[ September 19, 2007: Message edited by: Gabriel Claramunt ]

Gabriel
Software Surgeon
Michael Raymond Jr.
Ranch Hand

Joined: May 16, 2005
Posts: 178
Originally posted by Jethram Boda:
Diff between string and string buffer


//immutable
String s1 = "s1";
s1.concat(" - s1");
//new string created. " - s1" is NOT appended to original s1 value - a
//new value is created in memory for which you have no reference to.
System.out.println(s1); //only print s1

//mutable, value can be changed
StringBuffer s2 = new StringBuffer("s2");
s2.append(" - s2");
//s2 is actually appended here with " - s2" and still
// referenced by s2 variable
System.out.println(s2); //prints "s2 - s2"


Scooby Snacks for everyone...<br /> <br />SCJA, SCJP 1.4
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Diff between string and string buffer