How does the String class override the + operator?

前端 未结 7 2003
刺人心
刺人心 2020-11-22 12:25

Why in Java you\'re able to add Strings with the + operator, when String is a class? In theString.java code I did not find any implementation for this operator

7条回答
  •  温柔的废话
    2020-11-22 12:53

    First of all (+) is overloaded not overridden

    The Java language provides special support for the string concatenation operator (+), which has been overloaded for Java Strings objects.

    1. If left hand side operand is String it works as concatenation.

    2. If left hand side operand is Integer it works as addition operator

提交回复
热议问题