Option 1:
String newStr = someStr + 3 + \"]\";
Option 2:
String newStr = someStr + \"3\" + \"]\";
Which o
Assuming that someString is constant, both are constant expressions and will be evaluated at compile time. They will result in identical class files and runtime behavior.
Source: The Java Language Specification writes:
A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
Literals of primitive type and literals of type String (§3.10.1, §3.10.2, §3.10.3, §3.10.4, §3.10.5)
The additive operators
+and-(§15.18)...
Compile-time constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern.
If someString is not a constant, most modern compilers will use a StringBuilder, which is explicitly permitted by the Java Language Specification:
The result of string concatenation is a reference to a String object that is the concatenation of the two operand strings. The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string.
The String object is newly created (§12.5) unless the expression is a compile-time constant expression (§15.28).
An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.
For primitive types, an implementation may also optimize away the creation of a wrapper object by converting directly from a primitive type to a string.