Please tell me a real time situation to compare String
, StringBuffer
, and StringBuilder
?
Do you mean, for concatenation?
Real world example: You want to create a new string out of many others.
For instance to send a message:
String
String s = "Dear " + user.name + "
" +
" I saw your profile and got interested in you.
" +
" I'm " + user.age + "yrs. old too"
StringBuilder
String s = new StringBuilder().append.("Dear ").append( user.name ).append( "
" )
.append(" I saw your profile and got interested in you.
")
.append(" I'm " ).append( user.age ).append( "yrs. old too")
.toString()
Or
String s = new StringBuilder(100).appe..... etc. ...
// The difference is a size of 100 will be allocated upfront as fuzzy lollipop points out.
StringBuffer ( the syntax is exactly as with StringBuilder, the effects differ )
About
StringBuffer
vs. StringBuilder
The former is synchonized and later is not.
So, if you invoke it several times in a single thread ( which is 90% of the cases ), StringBuilder
will run much faster because it won't stop to see if it owns the thread lock.
So, it is recommendable to use StringBuilder
( unless of course you have more than one thread accessing to it at the same time, which is rare )
String
concatenation ( using the + operator ) may be optimized by the compiler to use StringBuilder
underneath, so, it not longer something to worry about, in the elder days of Java, this was something that everyone says should be avoided at all cost, because every concatenation created a new String object. Modern compilers don't do this anymore, but still it is a good practice to use StringBuilder
instead just in case you use an "old" compiler.
edit
Just for who is curious, this is what the compiler does for this class:
class StringConcatenation {
int x;
String literal = "Value is" + x;
String builder = new StringBuilder().append("Value is").append(x).toString();
}
javap -c StringConcatenation
Compiled from "StringConcatenation.java"
class StringConcatenation extends java.lang.Object{
int x;
java.lang.String literal;
java.lang.String builder;
StringConcatenation();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: aload_0
5: new #2; //class java/lang/StringBuilder
8: dup
9: invokespecial #3; //Method java/lang/StringBuilder."":()V
12: ldc #4; //String Value is
14: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
17: aload_0
18: getfield #6; //Field x:I
21: invokevirtual #7; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
24: invokevirtual #8; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
27: putfield #9; //Field literal:Ljava/lang/String;
30: aload_0
31: new #2; //class java/lang/StringBuilder
34: dup
35: invokespecial #3; //Method java/lang/StringBuilder."":()V
38: ldc #4; //String Value is
40: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
43: aload_0
44: getfield #6; //Field x:I
47: invokevirtual #7; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
50: invokevirtual #8; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
53: putfield #10; //Field builder:Ljava/lang/String;
56: return
}
Lines numbered 5 - 27 are for the String named "literal"
Lines numbered 31-53 are for the String named "builder"
Ther's no difference, exactly the same code is executed for both strings.