-1
String a="hello";
String b=a+"Bye";

How many Strings are formed?

From my understanding of Java. What happens in this code is:

String a="hello"; // hello is created in string pool
String b=a+"bye"; // new StringBuilder(a).append("bye") 

So totally 2 strings are to be created, right?

1.Hello
2.HelloBye (In the Heap)

Or does Java create 3?

1.Hello
2.Bye
3.HelloBye

If this is the case, does append method create the appending strings in the string pool?

Chaosfire
  • 4,818
  • 4
  • 8
  • 23

3 Answers3

2

String a = "hello";
JVM will create one string in the string pool. (FIRST STRING IN POOL)

Now, here comes the tricky part>
b = a + "bye";

Internally + operator uses StringBuffer for concatenating strings.
String b= new StringBuilder(a).append("bye").toString();
(The toString() method of StringBuilder is returning a new String which will be definitely in the Heap since it is created with new String(...). So "bye" will be SECOND STRING IN POOL.)

Now, b="hellobye" ("hellobye" will be THIRD STRING IN POOL)

  • What Java does internally for `a + "bye"` is an implementation detail. As far as I'm aware, recent versions will not use `StringBuilder`. – Mark Rotteveel Feb 17 '23 at 09:30
0
  • First string "hello" is created and added to the string pool.
  • Next, the String "Bye" is created and added to the string pool.
  • The concatenation of a and "Bye" results in a new String "helloBye", which is also added to the string pool.
  • A total of 3 Strings will be created in the pool: "hello", "Bye", and "helloBye".

When you create a new StringBuilder and append a string to it, the resulting string will not be added to the string pool. Instead, a new String object will be created in the heap memory to represent the combined string.

So, the code new StringBuilder(a).append("bye") will create one new String object in the heap memory to represent the combined string and one string in pool for "a".

Pankaj Yadav
  • 11
  • 2
  • 7
0

The only part of your question that can be answered with complete certainty is this:

Does append method create the appending strings in the string pool?

The answer is No. The result of a string concatenation that is not a constant expression is not placed in the string pool. At least not in any implementation of mainstream Java to date. However, there is no specification that actually guarantees this.

There are a couple of reasons why we don't know for sure how many strings are "formed".

  1. We don't know when the String objects corresponding to the literals are actually created. In some Java implementation they will be created (and interned) when the code is loaded. In others, the string creation could occur the first time this code is run.

  2. We don't know whether one or both of those literals are used by another class ... and hence whether this code is "forming" them.

  3. Depending on the Java implementation, interning a string (to put it in the string pool) may result in a new String object being created. So you might get a scenario where two String objects get "formed" for each literal.

In short there is enough ambiguity that we cannot be 100% sure of the precise number of strings that are created during the execution of that code.

Does it matter that we don't know for sure?

Frankly, no. It should make zero difference to the way that you write your code1. Let the Java compiler and runtime take care of it ... and use a recent version of Java to get the benefit of the work they have done on optimizing this.

1 - But it is still wise to avoid string concatenation loops. I don't know if they can be optimized.


In your commented version you wrote:

String a = "hello";     // hello is created in string pool
String b = a + "bye";   // new StringBuilder(a).append("bye") 

Both of those comments are questionable:

  • The "hello is created in string pool" comment is questionable for reasons that I gave above.

  • The new StringBuilder(a).append("bye") pseudo-code is questionable because that is an implementation detail. In Java 9 and later, expressions that involve string concatenations are translated to a invokedynamic bytecode. The JIT compiler generates native instructions directly. See How much does Java optimize string concatenation with +? for more information.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216