Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference  RSS feed

 
Saravanan Jothimani
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
 
Liam Tiarnach
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...
 
K Riaz
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
 
Marilyn de Queiroz
Sheriff
Posts: 9080
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.
 
David Harkness
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
 
Layne Lund
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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!