Junior — Middle
What action occurs when performing a string concatenation operation like String a = "Hello " + "World"?
sobes.tech AI
Answer from AI
When performing string concatenation operations like String a = "Hello " + "World";, the Java compiler combines these string literals at compile time into a single string — "Hello World". This is called constant string folding. As a result, the bytecode will contain only one string, not two, and the concatenation operation is not performed at runtime.
Example:
String a = "Hello " + "World";
System.out.println(a); // Outputs: Hello World
Thus, such code is more efficient because it does not create additional StringBuilder objects at runtime.