top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

In Java what is the difference between string vs stringbuffer ?

0 votes
609 views
In Java what is the difference between string vs stringbuffer ?
posted Jul 10, 2014 by Madhu Lakshmi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Strings are immutable . String buffer are mutable.
ie :

Once the String object is created it cannot be modified. Stringbuffer object can be modified.

String s = "abc"
s = s+""def" // a new string obect will either be created or assigned from string pool if exists.

StringBuffer st = new StringBuffer("abc");
st.append("def") ;// No new object is created st gets modified.

answer Jul 24, 2014 by Vikalp Kumar
Similar Questions
+4 votes

I was asked a question. If you had to build an XML structure as text, would you use StringBuffer or String? I thought String is more efficient that StringBuffer, but some of my friends say StringBuffer is more efficient than String.

I assume, that StringBuffer is a class, where as String is a base datatype and StringBuffer would have some easy (wrapper) methods for String. So, it is an add-on to string. Which should make StringBuffer heavier than String. Then how come using StringBuffer better than String?

0 votes

I read many times but i doesn't know the difference some one help me to understood clearly ?

...