cancel
Showing results for 
Search instead for 
Did you mean: 

Reg: String and StringBuffer

Former Member
0 Kudos

Hi All,

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

Thanks in advance.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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!!");

Answers (1)

Answers (1)

Former Member
0 Kudos