0
StringBuilder sb = new StringBuilder("mainWord"); sb.append("secondWord" + "thirdWord"); System.out.println(sb); 

The append method here accepts a String as a parameter. My question is, how does this not allocate memory for this String in the heap?

Our difference between String and StringBuilder is that a new object is created in memory for String, but not for StringBuilder. However, the append method still accepts a String as a parameter, which logically should be stored in memory somewhere.

5
  • 3
    I do not understand the question. When passed to the method, the memory for the String is already allocated. Commented May 9, 2024 at 10:47
  • Actually, you'll probably find the second line loads from the constant pool of the class file. e.g.: 11: ldc #14 // String secondWordthirdWord Commented May 9, 2024 at 10:49
  • 2
    Unfortunately "Strings on the heap" is a massive oversimplification of how Java handles strings specifically (interning and constant pools and soforth). A reason for using StringBuilder over String (especially in loops) is to prevent the creation of uneccessary amounts of String: ("one" + "two") + "three" (via a loop) will create the intermediate string representations, whereas StringBuilder will hold the data and compile it into a string at the end (usually in an exponentially growing character array, akin to ArrayList) via #toString, which #println implicitly calls on sb. Commented May 9, 2024 at 11:16
  • 1
    As @g00se points out, "one" + "two" (as literal code) will get compiled down to "onetwo": the compiler sees two constants and optimizes away the extra addition. "onetwo" is actually stored in "static memory": it exists within the compiled class file itself, and can be accessed as such. When you want to dynamically concatenate strings (in a way the compiler can't optimize), you will create a new String object when you finish adding them together. So loops (hard to pre-optimize) will create tens if not hundreds of intermediate strings. Commented May 9, 2024 at 11:23
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented May 10, 2024 at 5:48

1 Answer 1

0
StringBuilder sb = new StringBuilder("mainWord"); sb.append("secondWord" + "thirdWord"); System.out.println(sb); 

When compiling, for "mainWord" the compiler blocks memory that is statically initialized. It is not on the heap. It would do the same for "secondWord" and "thirdWord" but nowadays is smart enough to realize that adding two constants will always produce the same result. So it performs the calculation directly and blocks memory for "secondWordthrirdWord". Again it is not on the heap.

At runtime a StringBuilder is initialized with "mainWord". At this time the reference is passed, and Java is smart enough to not copy the whole String. When calling append with the other string, again a reference is passed. But now StringBuilder realizes it needs to copy both the data. So it reserves some memory on the heap and copies the content of both strings in there - creating the first buffer object on the heap.

Sign up to request clarification or add additional context in comments.

1 Comment

Note the bit about references isn't historically/always true (e.g. Java 7 had no such logic implemented).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.