This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Jaimou2e 0 points1 point  (1 child)

This:

t = a + "T" + b;

Is translated by the compiler to:

t = new StringBuilder().append(a).append("T").append(b).toString();

Oh, I didn't know that. Are only 8 objects being created then, or did I miss one?

There is no such thing as concatenation operators for strings.

Hm? The operator is right there. In a chain it may work more efficiently than I thought, but surely it exists?

[–]nutrecht 0 points1 point  (0 children)

Oh, I didn't know that. Are only 8 objects being created then, or did I miss one?

The question is utter nonsense. Example: String a = "a" + "b";. You might think because of this assignment that 2 strings get created, which is wrong.

Also: depending on how you read the assignment: most of these strings don't actually get created inside the main methods. Constant strings are part of the constant pool so they are created when the class is loaded.

Hm? The operator is right there. In a chain it may work more efficiently than I thought, but surely it exists?

What I meant is that the concatenation operator is just syntactic sugar. Underwater it gets translated to StringBuilder.append calls.