20

How many Java string objects will be created in the following statement?

String s = "abc" + "xyz"; 

I guess three?

2
  • In addition to the answer, also see: stackoverflow.com/questions/1842024/… Commented Aug 14, 2011 at 23:06
  • 4
    @Deep - please accept my answer - ie click the hollow tick symbol next to the answer (that is, if you think it's correct - many others seem to) Commented Aug 15, 2011 at 1:35

3 Answers 3

23

The compiler creates 1 String per JVM start, because the compiler can determine the resulting String at compile time, it is interned and statically stored in the JVM's String Table.


FYI, if the statement were concatenating variables (not determinable at runtime), 1 String would be created, but it would create a StringBuilder too. The code would compile to:

new StringBuilder().append(abcVar).append(xyzVar).toString()
Sign up to request clarification or add additional context in comments.

5 Comments

+1 for such an useful answer. Can you please explain "it creates a StringBuilder" or give any source so that I can read it in details. It seems interesting to me.
If I decompile the result of the class file produced from the code in the original example, I get: String a = "abcdef"; I guess my compiler took a shortcut.
Be aware that the compiler shortcuts will not apply / be the same if either of the Strings you append is a variable rather than a literal.
When two literals are concatenated they are automatically joined by compiler. So here it will be only one object. It's very important optimization when you consider e.g. strings broken into several lines for visibility.
@Bohemian please check the answer to this question : according to the JLS the string will be folded into 1 constant at compile time, just as Daniel noticed. The key here is that we're dealing with constants, not with variables.
9

The answer is one global String object per program run, and zero new String objects per statement execution. This is because the Java Language Specification says the expression "abc" + "xyz" is a compile-time constant [0], and that no new String object will be created when the statement is executed [1].

References

[0]: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#5313

Examples of constant expressions:

"The integer " + Long.MAX_VALUE + " is mighty big."

[1]: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.18.1

If only one operand expression is of type String, then string conversion is performed on the other operand to produce a string at run time. The result is a reference to a String object (newly created, unless the expression is a compile-time constant expression (§15.28)) that is the concatenation of the

Comments

-3

There are three ways to create strings in java

1) we can create a string just by assigning a group of characters to a string type variable

eg:

     String s;  //declare String type variable,  
     s="hello"; //assign a group of characters to it

2)we can create an object to string by allocating memory using new operator.This is just like creating an object to any class.

eg: String s =new String("Hello");

3) We can creating the strings is by converting the character arrays into string.

eg: char arr[] = {'p','r','a','s','h','a','n','t'};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.