Reputation: 43
How many string objects will be created by the following code?
String s = "Mahendra" + "Singh" + "Dhoni";
Will it create 4 string objects in the string pool or will it create more than that? Please explain in detail.
P.S. I have seen other examples and they deal only for 2 string objects.
EDIT : I see from the answers that in above case only 1 object will be created. Thanks a lot for some very good suggestions. Sorry to ask a stupid question but can you please tell in the below case how many objects will be created?
String s = "Mahendra";
s = s + "Singh" + "Dhoni";
2nd EDIT : Sorry again but I still have some doubt. I see in the comments that in the above case 3 objects will be created "Mahendra", "SinghDhoni" and "MahendraSinghDhoni". My doubt is shouldn't it be calculated from the left to right. Am I missing something - concept wise? Please help.
Also, in another case, if I twist this question, how will the number of objects change. I am asking this as in detail to clear it for lot of my colleagues as well. All the help is appreciated.
String s = "Singh";
s = "Mahendra" + s + "Dhoni";
Upvotes: 4
Views: 304
Reputation: 26926
This is a particular condition where compiler make optimization. Different compilers can do different optimizations. Here is what can happens:
Compiler see string concatenation of already known values. If the values are not used in other positions it can replace the concatenation with the final string so
String concatenation = "a" + "b" + "c";
is compiled to the equivalent byte code of
String concatenation = "abc";
In this case you will have just 1 string creation.
Over a certain number of string concatenations the compiler can decide to replace them with a StringBuilder
or StringBuffer
.
So
String a;
String b;
String c;
String d;
String e;
// Initialization and use of a, b, c, d, e
String concat = a + b + c + d + e;
is compiled to the equivalent bytecode of the following java code:
String a;
String b;
String c;
String d;
String e;
// Initialization and use of a, b, c, d, e
String concat = new StringBuilder(a).append(b).append(c).append(d).append(e).toString();
In this case you will have 6 strings creation: a, b, c, d, e, and the string obtained from a + b + c + d + e
If the creation of StringBuilder is not convenient the compiler can apply a normal string concatenation so the following code
String a;
String b;
String c;
// Initialization and use of a, b, c
String concat = a + b + c;
is compiled to the a bytecode equivalent to the following java code
String a;
String b;
String c;
// Initialization and use of a, b, c
String concat = a.concat(b).concat(c);
Here is the java implementation of concat code:
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
char buf[] = new char[count + otherLen];
getChars(0, count, buf, 0);
str.getChars(0, otherLen, buf, count);
return new String(0, count + otherLen, buf);
}
As you can see at the last line of concat a new String is created for each concatenation. In this case you will have 5 strings created: a, b, c, a + b, and a + b + c
Note: any different compiler can choose different internal optimization from source code to compiled code. What you need to know is that in the worst case you will have the generation of 1 string for each starting string and 1 string for each concatenation (each +). So if you have 3 strings concatenated with 2 concatenation operations you will have a maximum of 5 string creation.
Upvotes: 1
Reputation: 36304
If your code was :
public static void main(String[] args) {
String s = "Mahendra" + "Singh" + "Dhoni";
System.out.println(s);
}
Look at the byte code :
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=2, args_size=1
0: ldc #16 // String MahendraSinghDhoni --> This line
2: astore_1
3: getstatic #18 // Field java/lang/System.out:Ljav
/io/PrintStream;
6: aload_1
7: invokevirtual #24 // Method java/io/PrintStream.prin
ln:(Ljava/lang/String;)V
10: return
As you see the compiler is adding All three strings during compile time. So there will be only one string on the String constants pool thats all.
EDIT : based on the edited question :
"Singh" + "Dhoni"
will be added during compile time. So there will be totally 3 String objects.
"Mahendra"
and "SinghDhoni"
will be on String constants pool and then
mahendraSinghDhoni
will be on heap.
Upvotes: 6
Reputation: 3057
This will create only 1 string s in string pool. Please read the difference bwt String object and literal it will help you understand.
Upvotes: 1