This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please give me a detailed difference between String and StringBuffer

Thanks in Advance

Saravanan
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Saravanan Jothimani:
Hi,

Please give me a detailed difference between String and StringBuffer

Thanks in Advance

Saravanan



Hmmm... to much typing...

how about this link...

http://java.sun.com/docs/books/tutorial/java/data/strings.html

from the Sun's Java Tutorial...
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String is immutable (it can't change) whilst a StringBuffer is not (it can change). Its a popular inteview question. When you invoke methods on a String object, you may think you can edit it, but you are actually getting a new String object back.
 
Saravanan Jothimani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class stringbufferexample
{
String str = "TATA";
StringBuffer strbuff = new StringBuffer();
String strone = "Infotech";
StringBuffer strbuffone = strbuff.append("ICICI");
public void print()
{
System.out.println(str);
System.out.println(strone);
System.out.println(strbuff);
System.out.println(strbuffone);
}
public static void main(String args[])
{
stringbufferexample sbe = new stringbufferexample();
sbe.print();
}
}

My out put is

TATA
Infotech
ICICI
ICICI

Why I am getting the output like this
System.out.println(str); - TATA -Correct
System.out.println(strone); - Infotech - Correct
System.out.println(strbuff); - ?. - I have createad an object
System.out.println(strbuffone); -ICICI - Correct - Because I am appending a string.

Pls help me out
I am little bit confused.

Thanks in Advance

Saravanan
 
Sheriff
Posts: 9087
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key is in this line
StringBuffer strbuffone = strbuff.append("ICICI");
Although you are assigning the result to strbuffone, you have also changed the original strbuff object. When you print strbuff, you get the modified StringBuffer object, the one with "ICICI" appended.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason StringBuffer.append() is confusing is that the StringBuffer it returns is not a new StringBuffer with the text appended but the same StringBuffer to support method chaining.
 
Saravanan Jothimani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also want to look at this.

Layne
 
Saravanan Jothimani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If I am using
Final StringBuffer s = new StringBuffer ("Hello");, Can I append this object s using append();

Thanks in Advance

Saravanan
 
author and iconoclast
Sheriff
Posts: 24219
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Saravanan Jothimani:

If I am using
Final StringBuffer s = new StringBuffer ("Hello");, Can I append this object s using append();



Yes. The variable "s" is final -- i.e., it can't be made to point to another StringBuffer -- but the StringBuffer itself is mutable. There's no equivalent to C++'s "const" in Java.
 
Saravanan Jothimani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest Friedman-Hill

Regards

Saravanan
 
There's a way to do it better - find it. -Edison. A better tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!