Why does + work with Strings in Java?

前端 未结 6 1573
一向
一向 2020-12-19 06:26

Java can\'t do operator overloading, but + works okay for String and Integer and some other classes. How is this possible?

6条回答
  •  难免孤独
    2020-12-19 06:37

    The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings.

    String s = "string 1" + "string 2";
    

    What actually is execute is

    (new StringBuilder()).append("string 1").append("string 2").toString()
    

提交回复
热议问题