Search code examples
javasyntaxoperatorsincrementunary-operator

What is the difference between += and =+?


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


Solution

  • i += 4;
    

    means

    i = i + 4;  // increase i by 4.
    

    While

    i =+ 4;
    

    is equivalent to

    i = +4;   // assign 4 to i. the unary plus is effectively no-op.
    

    (See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.3 for what a unary + does.)