File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String vs 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 "String vs String Buffer" Watch "String vs String Buffer" New topic
Author

String vs String Buffer

Nicky narayan
Greenhorn

Joined: Jul 01, 2009
Posts: 27

What is the exact difference the String and String Buffer ?

As the definion said abt String is Immutable(cant be changeble)
But, in the example:

String str="Nicky";
out.println(str); // it prints Nicky
DONE MODIFICATION ON THE ORIGINAL STRING LIKE :-

str="n"+str;
out.println(str); // it prints nNicky

Here my doubt is str is appended with 'n' letter.

then how can we call it as Immutable string.

I observed one diff b/w string and String buffer is -

String str="nicky";

StringBuffer str1="Narayan"; // gives compile time error

Means StringBuffer should not accept Literals..


I need to know the exact difference .Please tell me any one knows this.




Nicky
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Nicky narayan wrote:
StringBuffer str1="Narayan";


is Equivalent to:



is Equivalent to:



they are two different things you cannot use them like that,

Strings are immutable,
http://www.coderanch.com/t/473540/Beginning-Java/java/IF-strings-are-immutable-why#2121322


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Tauri Valor
Ranch Hand

Joined: Aug 03, 2005
Posts: 166
String is immutable because whenever the value of a String is changed a new object is created and make the variable reference this new object. Appending a String to another existing one is the same kind of deal: a new String containing the stuff from both is created and the old one is dropped.


A Moment's insight is sometimes worth a Life's experience.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Nicky narayan wrote:String str="Nicky";
out.println(str); // it prints Nicky
DONE MODIFICATION ON THE ORIGINAL STRING LIKE :-

str="n"+str;
out.println(str); // it prints nNicky

In this example, you are not modifying the original string. What happens is that you create a new String object with the expression "n" + str, and you assign that new String object to the variable str. The old String object that str was referring to is discarded.

Class String is indeed immutable, which means that the content of a String object cannot be modified after it has been created. But ofcourse you can make variables of type String refer to a different String object.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nicky narayan
Greenhorn

Joined: Jul 01, 2009
Posts: 27
Means Original str(Nicky) is stored on 1 location.
Modified str(nNicky) is stored in 2nd location.

when ever I am trying to access the 'str' variable i will get "modified string"(nNicky) rite ?

Is there any chance to get the original string (Nicky)?


Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
You can get at the original String by retaining a reference (as usual) or by repeating the original literal "Nicky" in your code.

The real difference is that String is not intended for changes (being immutable) and StringBuilder (usually better than StringBuffer, but the two are very similar) is intended for changing the text.
Rajeev Trikha
Ranch Hand

Joined: Jan 29, 2010
Posts: 85
If you run the following code the difference between String and StringBuffer becomes very clear

String testStr = "My Test String";
StringBuffer tb = new StringBuffer("My Test String");
testStr.concat(" is changed");
tb.append(" is changed");
System.out.println("testStr =>" + testStr);
System.out.println("tb =>" + tb);
testStr = testStr.concat(" is changed");
System.out.println("testStr =>" + testStr);

It produces the output:

testStr =>My Test String
tb =>My Test String is changed
testStr =>My Test String is changed

Because String is immutable when we carried out the first concat(0 it created a new string but there is nothing referring to it. Our original String remained intact. Whereas the similar sort of logic with StringBuffer() changed the value.

StringBuffer class are synchronized version of StringBuilder. You would normally use StringBuilder as it performs much better. StringBuffer is required for thread-safe operations.


Rajeev Trikha (SCJP 6)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String vs String Buffer