How many memory locations will it take to have a string concatenation?
String myStringVariable = "Hello";
In following two statements :
String s = "ABC" + "Hello" + "DEF";
and
String s = "ABC";
s = s + "Hello";
s = s + "DEF";
and
String s = "ABC" + myStringVariable + "DEF";
Which will consume more memory? In which of the case StringBuilder is useful to the most?
First statement will be converted by compiler into String s = "ABCDEF";
so there will be no concatination
Second statement will be converted by compiler into this code (or something like this)
String s = "ABC";
StringBuilder sb = new StringBuilder(s);
sb.append("DEF");
s = sb.toString();
StringBuilder
will be helpful in neither case, as written - the compiler will reuse the same StringBuilder
instance when possible. It's marginally helpful when you're adding strings in a loop. (I say "marginally" because most of the time you're not really adding that many strings, nor are they that long. The difference will show up in microbenchmarks but your program isn't one, unless you're writing a templating engine or something like that.)
Creating garbage StringBuilder
s doesn't "waste memory" as much as cause GC pressure. A generational scavenger GC such as Java's is, in fact, designed to make creating lots of very short-lived garbage objects efficient. I wouldn't worry about it unless you're actually spending too much time in GC.
Copying the String
contents into the StringBuilder
s repeatedly is also wasteful, but once again, unlikely to make a significant impact outside a loop.
I create class Main
:
public class Main {
public String foo() {
String s = "ABC" + "DEF";
return s;
}
public String foo1() {
String s = "ABC";
s = s + "DEF";
return s;
}
}
and decompile it:
public java.lang.String foo();
Signature: ()Ljava/lang/String;
Code:
0: ldc #2 // String ABCDEF
2: astore_1
3: aload_1
4: areturn
public java.lang.String foo1();
Signature: ()Ljava/lang/String;
Code:
0: ldc #3 // String ABC
2: astore_1
3: new #4 // class java/lang/StringBuilder
6: dup
7: invokespecial #5 // Method java/lang/StringBuilder."<init>":()V
10: aload_1
11: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
14: ldc #7 // String DEF
16: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
19: invokevirtual #8 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
22: astore_1
23: aload_1
24: areturn
Method foo
make concatenation string compile-time but method foo1
concatenate string at run-time and use StringBuilder
.
First example use less memory.
I use Oracle JDK 1.7_45 with default properties.
String s = "ABC" + "DEF";
That resolved at compile time and no StringBuilder
used to concat.
and
String s = "ABC";
s = s + "DEF";
In this case StringBuilder(string...).toString()
to resolve the String
at run time.
So you need to use StringBuilder
, In your case it's negligible performance difference.
do not use much concatenation's with "+", cannot do much harm in lesser amount of concatenations. If you are dealing with larger amount prefer to use StringBuilder with append method.
You can see the difference if you are concatenating String's in Large extent with +
, then go for StringBuilder
append.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With