How many string objects will be created by the following code?
String s = \"Mahendra\" + \"Singh\" + \"Dhoni\";
Will it create 4 string obj
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.
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.
This will create only 1 string s in string pool. Please read the difference bwt String object and literal it will help you understand.