Skip to Content
avatar image
Former Member

Reg: String and StringBuffer

Hi All,

What is the difference b/w String variable and StringBuffer variable in their properties and in working?

Thanks in advance.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Oct 12, 2007 at 08:33 AM

    1.String variable is immutable. So once you declare it you are not able to modify it. But StringBuffer class is mutable. You can change the content of StringBuffer variable

    Mutable object are those object whose value can be changeable

    while if you are not allowed to change the value of object then it is called called immutable

    2.String Class object allocate fix memory if we assign different object to the same reference then previousely reference will be lost or we can not access previous value

    but stringBuffer keeps some extra memory

    3.String name= new String("what");

    name.setCharAt(2,'a');//gives error.

    Stringbuffer name = new stringbuffer("what");

    name.setcharAt(2,'a');//does not give error and modify the string as 'waat'.

    4.The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations. In String manipulation code, character strings are routinely concatenated. Using the String class, concatenations are typically performed as follows:

    String str = new String ("Stanford ");

    str += "Lost!!";

    If you were to use StringBuffer to perform the same concatenation, you would need code that looks like this:

    StringBuffer str = new StringBuffer ("Stanford ");

    str.append("Lost!!");

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 12, 2007 at 08:28 AM
    Add comment
    10|10000 characters needed characters exceeded