Possible Duplicate:
Questions about Java’s String pool
I have a doubt in java Strings object creation.
String s1 = "Hello"+"world";
String s2 = s1+"Java";
in this program how many String objects will be created and how ?please explain it. Thanks.
Possible Duplicate:
Questions about Java’s String pool
I have a doubt in java Strings object creation.
String s1 = "Hello"+"world";
String s2 = s1+"Java";
in this program how many String objects will be created and how ?please explain it. Thanks.
 
    
     
    
    The answer is 3
Two String objects will be created once per JVM start:
Both will be interned, because they are constants (known at compile time).
They will be reused every time this code runs. A StringBuilder will be created to concatenate the two String above. References to them will be assigned to s1 and s2.
Here's the bytecode for the code:
   0:   ldc #37; //String Helloworld
   2:   astore_1
   3:   new #39; //class java/lang/StringBuilder
   6:   dup
   7:   aload_1
   8:   invokestatic    #41; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
   11:  invokespecial   #47; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
   14:  ldc #50; //String Java
   16:  invokevirtual   #52; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   19:  invokevirtual   #56; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   22:  astore_2
   23:  return
 
    
    You can't really say, how many Strings are created, since there's several differences due to the different implementations of the JVM.
As String is an immutable class, the naive answer is 5. But with some optimization (e.g. using a StringBuffer/  StringBuilder there would only be 2 Strings. 
As concats would be summarized via append()-calls.
Edit: As the're some different answers here an explanation why I said 5:
 
    
     
    
    if you look at the compiled code, you can easily guess:
String s1 = "Helloworld";
String s2 = (new StringBuilder(String.valueOf(s1))).append("Java").toString();
We can't accurately know by just looking at source code as many optimizations are done by the compiler before execution.
Here we see that 1 String object is created for s1, and another String object for s2. Here 2 string literals are there in the string pool: "Helloworld" and "Java"
 
    
    If you decompile your Program.class you will see the real code
String s1 = "Helloworld";
String s2 = (new StringBuilder(String.valueOf(s1))).append("Java").toString();
10 objects it seems, because inside each String there is char[] value this is a separate object + another char[] inside StringBuilder
 
    
    The answer is 3.
You can view the deassembled result by:
javap -verbose YourClass
The Constant pool includes:
...
const #17 = Asciz       Helloworld;
...
const #30 = Asciz       Java;
...
It means two strings ("Helloworld" and "Java") are compile-time constant expression which will be interned into constant pool automatically.
The code:
Code:
 Stack=3, Locals=3, Args_size=1
 0:   ldc     #16; //String Helloworld
 2:   astore_1
 3:   new     #18; //class java/lang/StringBuilder
 6:   dup
 7:   aload_1
 8:   invokestatic    #20; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
 11:  invokespecial   #26; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
 14:  ldc     #29; //String Java
 16:  invokevirtual   #31; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
 19:  invokevirtual   #35; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
 22:  astore_2
 23:  return
It indicates that s2 is created by StringBuilder.append() and toString().
To make this more interesting, javac can optimize the code in constant folding. You can guess the count of strings created by the following code:
final String s1 = "Hello" + "world";
String s2 = s1 + "Java";
"final" means s1 is constant which can help javac to build the value of s2 and intern s2. So the count of string created here is 2.
 
    
    Yup, there will be be five String objects created. Strings are immutable; followings are the steps - 1. First "Hello" 2. then another objects "Hello World" 3. then another object "Java" 4. then s1+"Java" and then finally s2 would be created.
 
    
    Actually no String objects will be created just two String literals. When Strings are initialized like you have they are literals not objects. If you wanted to create String objects you would do the following
String a = new String("abcd");
