Java does not support user defined overloaded operators. Java does allow the use of using the + operator between two String Objects. When this happens, is there some internal/built in overloading to the + happening or what happens in order for two String objects to be added with the use of +?
-
read this https://atos.net/en/blog/will-applications-run-faster-java-9-10-11 .... and this https://www.baeldung.com/java-string-performance – JavaMan Nov 12 '20 at 06:42
-
BTW, the actual implementation wich is called in this situation has changed a few times and is meanwhile highly optimized in compiler and JIT, it is therefore not only easier to read, but usually also faster than dealing with StringyBuilder append yourself. The only exception to that is, if you have concatenation happening over multiple statements or across loop iterations. – eckes Nov 27 '20 at 12:32
2 Answers
There is no overloading of operator. The behavior of the +
operator is well-defined in the Java Language Specification, section 15.18. Additive Operators:
The operators
+
and-
are called the additive operators.
If the type of either operand of a
+
operator isString
, then the operation is string concatenation.
Section 15.18.1. String Concatenation Operator +
then goes on saying:
If only one operand expression is of type
String
, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time.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.

- 154,647
- 11
- 152
- 247
"+" operator (in this context) is not "overloaded", it is pre-defined operator, called String Concatenation Operator.
15.18.1 String Concatenation Operator
+
If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time. 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.
In other words, when Java sees
String res = stringObj + someObj;
it replaces the expression with code that constructs the resulting string by concatenating an existing string value with someObj.toString()
.

- 23
- 7