Reputation: 2003
I was asked this question today in an interview. Can someone explain me the right answer ?
Here is the code.
String s1= "hellow";
String s2= "Hellow again";
System.out.println(s1+s2);
How many strings are created in the above code ? I think it will be 3.Any suggestions?
Upvotes: 1
Views: 2087
Reputation: 2960
And the right answer for an interview question is probably not "3
" but some (but not necessarily all) of the discussion above. They'd definitely want you to know that +
creates a string; probably want some awareness that the constants s1
and s2
are created/exist as compile-time constants; and may be suitably impressed if you knew about the effect of adding final
. Such questions are often not about getting the right answer, but thinking about them in the right way.
Upvotes: 0
Reputation: 719199
The answer is implementation dependent, and it also depends on the context ... and what you mean by creation.
The execution of those lines of code creates one String
object for the concatenation, and possibly others within the println
call1. These creations would happen each time your application executes those lines.
At least a further two String objects will be created when the code is loaded and String objects are created for the String literals. However, that is a once off ...
1 - In some class libraries, println(s)
is implemented as print(s + newline)
. Hence the println
call may create another String
object.
Upvotes: 0
Reputation: 11867
This answer is essentially an extension of arshajii's answer
The answer all depends on what your interviewer(s) meant by "create", and technically also depends on what other code is present.
If you disassemble the bytecode generated by just that snippet, you get this:
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String hellow
2: astore_1
3: ldc #3 // String Hellow again
5: astore_2
6: getstatic #4 // Field java/lang/System.out:Ljava/io/PrintStream;
9: new #5 // class java/lang/StringBuilder
12: dup
13: invokespecial #6 // Method java/lang/StringBuilder."<init>":()V
16: aload_1
17: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
20: aload_2
21: invokevirtual #7 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
24: invokevirtual #8 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
27: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
30: return
Because "hellow"
and "Hellow again"
are string literals in the source code, they get placed into the constant pool at compile time, and so are present at program startup. As you can see, the strings "hellow"
and "Hellow again"
are simply loaded (ldc == load constant
). They are not created by the above code snippet. The only String
that's created is the one from the StringBuilder
.
Now, if you declare the fields final
, you get this:
public static void main(java.lang.String[]);
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String hellowHellow again
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
Based on this, you can also argue that no String
objects are created by the above code snippet, as the compiler can optimize this statement. This is probably not the answer that the interviewers were looking for, since it depends on final
being present.
Upvotes: 2
Reputation: 129537
The string literals "hellow"
and "Hellow again"
are in the string pool.
Now when we concatenate with s1 + s2
, what really happens is the following:
new StringBuilder(s1).append(s2).toString()
which itself creates a new String
(see for yourself). So it depends what you mean by "create"; if you're asking how many String
objects exist at the very end of the snippet, then the answer is 3. But note that the string produced by s1+s2
is not retained and is therefore eligible to be garbage collected after it is printed.
Upvotes: 4
Reputation: 1947
3 is the answer.
But use a StringBuilder (as StringBuffer is useless thread safe version of the StringBuilder).
You should use the StringBuilder.append method, instead of the + operator.
Upvotes: -1
Reputation:
The operator hierarchy in Java states, that String concatenation is being evaluated before a method call. Therefore the expression in brackets first creates a new String and is then being written to the standard output.
My answer: 3
Upvotes: 0
Reputation: 473
Strings are immutable so s1+s2 creates a new String instance. If you want to avoid this you should use a StringBuffer.
Upvotes: 0