whoismaikl
whoismaikl

Reputation: 314

What will happen in heap concatenate same strings

what will happen if in heap if i execute this code:

String text = "a" + "b" + "c";
String text = "a" + "a" + "a";

I know that in first variant there will be three String objects created. But in second one? Is there something being optimized in JVM?

Upvotes: 3

Views: 108

Answers (2)

davidxxx
davidxxx

Reputation: 131316

To answer to the question the JLS gives two information :

The JLS (jls-15.28) states that :

Constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern.

It means that is you use constant expressions in your code they will be put in the cache that you refer to.

But String text = "a" + "b" + "c"; is one constant expression or are they three constant expressions as you wonder?

We have the answer to this question in another point of the JLS.

The JLS (jls-3.10.5, point 4) states indeed that :

Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.

Example of constant expression :

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

It means that as soon as the compile time the expression above is one String literal.

In a some way, it looks like to what you are doing in your sample example:

String text = "a" + "b" + "c";
text = "a" + "a" + "a";

Consequently, String text = "a" + "b" + "c"; creates a single String, that is "abc" as the constant expression "a" + "b" + "c" is treated as a single literal.

It is exactly the same thing for text = "a" + "a" + "a"; that also is a single String literal for the compiler.

So finally, with you two instructions you get two Strings interned in the heap.

Upvotes: 2

Andy Turner
Andy Turner

Reputation: 140309

There are actually just 1 string literal each. As described JLS Sec 3.10.5:

A long string literal can always be broken up into shorter pieces and written as a (possibly parenthesized) expression using the string concatenation operator + (§15.18.1).

So, "a" + "b" + "c" and "a" + "a" + "a" are identical to "abc" and "aaa" by specification.

Additionally, these are constant expressions, as described in JLS Sec 15.28:

A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

  • Literals of primitive type and literals of type String (§3.10.1, §3.10.2, §3.10.3, §3.10.4, §3.10.5)

...

Constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern.

Upvotes: 2

Related Questions