What is the difference between += and =+?

后端 未结 9 1693
夕颜
夕颜 2020-12-03 14:07

What is the difference between += and =+? Specifically, in java, but in general also.

9条回答
  •  旧时难觅i
    2020-12-03 14:58

    Specifically, in java, but in general also.

    In Java x += ; is equivalent to x = x + ( ); where the + operator may be the arithmetical add operator or the string concatenation operator, depending on the type of x. On the other hand, x =+ ; is really an ugly way of writing x = + ; where the + is unary plus operator ... i.e. a no-op for numeric types and a compilation error otherwise.

    The question is not answerable in the general case. Some languages support a "+=" operator, and others don't. Similarly, some languages might support a "=+" operator and others won't. And some languages may allow an application to "overload" one or other of the operators. It simply makes no sense to ask what an operator means "in general".

提交回复
热议问题