Search code examples
javastringoperator-overloadingstring-concatenation

+ operator for String in Java


I saw this question a few minutes ago, and decided to take a look in the java String class to check if there was some overloading for the + operator.

I couldn't find anything, but I know I can do this

String ab = "ab";
String cd = "cd";
String both = ab + cd; //both = "abcd"

Where's that implemented?


Solution

  • From the Fine Manual:

    The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

    See String Concatenation in the JLS.