on 10-12-2007 9:24 AM
Hi All,
What is the difference b/w String variable and StringBuffer variable in their properties and in working?
Thanks in advance.
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!!");
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
24 | |
12 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.