Use of =+ operator in Java?

This question already has an answer here:

  • What is the difference between += and =+? 8 answers
  • What is the purpose of the =+ operator in Java? [duplicate] 2 answers

  • int i =+ 2;
    

    It is positive 2(+2) assignment to variable i. It is more miningful or understandable if your write like -- int i = +2;

    One more example -

    int i = 2;
    i=+3;
    System.out.println(i);
    

    It prints 3.

    + Unary plus operator; indicates positive value (numbers are positive without this, however)

    More example - http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/java/nutsandbolts/examples/UnaryDemo.java


    I believe what you mean by =+ is really +=.

    Your code is assigning the value of +2 (positive 2) to the integer.

    For example:

    int x =+ 4;
    x =+ 8;
    Console.WriteLine(x.ToString());
    Console.ReadLine();
    

    Will print "8", not 12. This is because you are assigning x to +4 and then +8.

    If you are asking about what += does, it is a shorthand to takes the initial variable and add to it.

    x += 8
    

    is the same as

    x = x + 8
    

    By changing the previous example form =+ to += give us:

    int x = 4;
    x += 8;
    Console.WriteLine(x.ToString());
    Console.ReadLine();
    

    Will print "12".


    Upon saying:

    int i =+ 2;
    

    + acts as a unary operator.

    To elaborate, it sets i to a positive value 2 .

    EDIT: Your update says:

    int i =- -2;
    

    produces 2. Why?

    In this case, it implies that i=-(-(2)) . Note that using a unary minus operator might produce unexpected results when the value is, say, Integer.MIN_VALUE .

    链接地址: http://www.djcxy.com/p/73944.html

    上一篇: javascript if语句如果url包含子字符串

    下一篇: 在Java中使用= +运算符?