Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String vs String Buffer

 
Nicky narayan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.



 
salvin francis
Bartender
Posts: 1263
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tauri Valor
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nicky narayan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48433
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic