2

Based on my understanding on String objects, that every string literal is being added/created on the String Constant Pool.

String a = new String("hello world");

two objects are being created, one is the "hello world" being added on the constant pool and the other object is the new instance of String. Does the same principle is being applied when it comes to StringBuilder?

StringBuilder b = new StringBuilder("java is fun");
b.append(" and good");

in the StringBuilder example does it mean that there are 3 objects being created? The first one is the new instance of StringBuilder and the other 2 objects are the string literals "java is fun" and " and good"?

3 Answers 3

3

Yes, your understanding is correct. (But see below.) String literals go in the constant pool, while the new String(...) and new StringBuilder(...) create additional objects. (The StringBuilder also creates an internal character array object, so that there are at least four objects involved in the second example.) Note that the call to b.append(...) may internally create another object and some garbage, but only if the internal character array used by b needs to expand.

EDIT: As @fdreger points out in a comment, the string objects corresponding to the string literals are created not at run time, but rather at the time the class is created during the creation and loading phase of the class life cycle (as described in the Java Language Specification; see also the section on the runtime constant pool).

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

3 Comments

new StringBuilder() also creates a char[] to hold the contents of the string builder, so the understanding of the OP that 3 objects are being created is incorrect: there are at least 4 objects: StringBuilder, char[], 2xstring on constant pool. And possibly some more in the append call.
@ErwinBolwidt - Excellent point. I updated my answer regarding how many objects are created. (I had already addressed the possible effects of the call to append.)
The String class also has an internal char[].
1

The strings are created when the class is loaded, not when the actual code is executed. You may think of them as part of your code, exactly as any other literal value.

So in your second example:

  • two strings are created when the code is loaded for the first time
  • no strings are created during the execution of the code.

8 Comments

No string is created during the execution of new String(...)? Care to explain that? (I think you'll find that a == "hello world" will evaluate to false.)
@TedHopp "No string is created during the execution of new String(...)?" Where do you see a new String in the second example?
Oops. I misread your post. Sorry about that. However, OP was asking about object creation, not string creation.
@TedHopp "Oops. I misread your post." My post? :P. But you're right about the question. That is something that partly missing in fdregers answer.
Yes, I misread your post when I skipped over the qualifier "in your second example".
|
0

Yes, you're right.

"java is fun" and " and good" are going to be stored in the constant pool. I would call them objects, but yes, it's more like it.

b in this case is going to be an object with a limited lifespan. It may actually store its own copy of both strings in its buffer to work with, so that originals won't be modified.

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.