• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Difference

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9103
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest Friedman-Hill

Regards

Saravanan
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic