问题
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?
回答1:
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();
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
Which will consume more memory?
The java specification 15.8.1 String Concatenation Operator +:
The String object is newly created (§12.5) unless the expression is a compile-time constant expression (§15.28).
An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.
First statement,
String s = "ABC" + "Hello" + "DEF";
This expression is a compile-time constant expression. Will be done in one step. No object is created in run-time.
Second statement:
String s = "ABC"; s = s + "Hello"; s = s + "DEF";
this statement will create a new string containing
"ABCHELLO"
. Third statement will create a new string containing"ABCHELLODEF"
. This string concatenation will happen using aStringBuilder
.Third statement
String s = "ABC" + myStringVariable + "DEF";
unlike the first statement, this statement Will create one new instance at run-time as it is not a compile-time constant expression.
But using StringBuilder
for concatenating such three string won't result in performance gain, unless you are working with number of strings with which counting does matter.
回答6:
StringBuilder is useful when you want to play with string. dynamically creating string by combining string. it will occupy in one object only.
if you use string every time it any concatenation will create new object in memory.
来源:https://stackoverflow.com/questions/20261997/how-many-memory-locations-will-it-take-to-have-a-string-concatenation