Reputation: 227
class StringTest1
{
public static final void main(String... a)
{
String str1="JAVA";
String str2="WORLD";
String str3=str1+str2;
}
}
How many objects will be created in this process? I think 3 objects will be created.
class StringTest2
{
public static final void main(String... a)
{
String strTest="JAVA"+"WORLD";
}
}
How many objects will be created in this process?How many objects are accessible? somebody told me that "JAVA"+"WORLD" is an expression and it would not stored in string pool.
Upvotes: 0
Views: 174
Reputation: 2200
In first case three objects will get created and all of them will be accessible since you are having live references to them.
In second case if you are doing something like
String str1="JAVA";
String str2="WORLD";
String strTest="JAVA"+"WORLD";
Then there will be three objects that will get created. Here "JAVA" will be in String object pool while you are creating strTest so it will not allocate memory for that and it will take direct reference from the pool. Similarly for "WORLD" too. And then a new object will get created out of these two objects and it will be assigned to strTest. The accessible objects here will be 3.
But if you are doing something like
String strTest="JAVA"+"WORLD";
Here only ONE object is accessible to you which is "JAVAWORLD". References to "JAVA" and "WORLD" are there in pool but there is no way you can directly access them. As @Vivin Paliath mentioned "JAVA" and "WORLD" will be interned to form a new Object "JAVAWORLD". Hence, only two objects gets created.
For more information related to String object pool see here
Hope this helps.
Upvotes: 0
Reputation: 95508
In the first example, you will end up with three String
objects. You can actually see this in the bytecode.
Assume you have the following code:
public class StrTest {
public static void main(String[] args) {
String str1 = "JAVA";
String str2 = "WORLD";
String str3 = str1 + str2;
String strTest = "JAVA" + "WORLD";
}
}
The resulting bytecode is:
public class StrTest {
public StrTest();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String JAVA
2: astore_1
3: ldc #3 // String WORLD
5: astore_2
6: new #4 // class java/lang/StringBuilder
9: dup
10: invokespecial #5 // Method java/lang/StringBuilder."<init>":()V
13: aload_1
14: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
17: aload_2
18: invokevirtual #6 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
21: invokevirtual #7 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
24: astore_3
25: ldc #8 // String JAVAWORLD
27: astore 4
29: return
}
You can see that the Java compiler has created two String
instances for "JAVA"
and "WORLD"
in the string pool. To append these two together, it created a StringBuilder
instance and appended the values. After that, it calls toString()
on that instance, which creates a new String
instance with "JAVAWORLD"
inside it.
In the second case, you only end up with one String
instance because the Java compiler is smart enough to see that what you basically have is a constant, and so it performs some compile-time optimization by evaluating that expression and creating one String
instance in the string pool that contains "JAVAWORLD"
. The reference to that instance is then assigned to strTest
.
With regard to the strings "JAVA"
, "WORLD"
, and "JAVAWORLD"
, these are basically interned strings. Since strings are immutable in Java, you only need one reference to a unique instance which can be reused in multiple locations in the code. This is basically a way to save memory.
So to sum up:
String
instances: two in the pool, one newly-constructed after appending (a StringBuilder
instance was also created to append the two strings, leading to 4 objects total).Upvotes: 5
Reputation: 15145
String str1="JAVA";
String str2="WORLD";
String strTest="JAVA"+"WORLD";
Each of the three lines will result in the creation of one String object. But it will always be the same object if the code is resulted multiple times. Also, each String object will contain a char[]
object, so technically, it's 2 objects per line.
String str3=str1+str2;
Assuming this cannot be optimized by the javac
, it will compiled as
String str3 = new StringBuilder(str1).append(str2).toString();
so, it will not only create a String, but also a StringBuilder which is not accessible in the code. Thus, in worst case 4 objects are created: String, StringBuilder and to char arrays (because it has to be resized during append
)
Upvotes: 1
Reputation: 14439
In the first case compiler can not be sure what values str1
and str2
will have, so it is not a constant expression and thus it will be evaluated in runtime. There will be three objects (two from string literals and one created as its concatenation).
In the second case compiler is able to compute result, so there will be just one object, as if you wrote "JAVAWORLD"
Upvotes: 0