1

I've the following code:

public class Operators {
    public static void main(String[] args) {
        int i =+ 2;
        System.out.println(i);
    }
}

Upon executing I'm getting the following output: 2

So what does =+ operator actually does here?

EDIT:

As some answered, it is assigning +2 to i, consider the following code:

public class Operators {
    public static void main(String[] args) {
        int i =- -2;
        System.out.println(i);
    }
}

So in above case, output should be -2. But I'm getting 2

So I suppose, it is -(-2), which gives 2. Right?

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
  • `=+` is two operator `=` and prefix `+` one single. – Grijesh Chauhan Oct 03 '13 at 05:25
  • 4
    It would probably make better sense if it was written as `i = +2`, which is the same as `i = 2`. But if I said `i = -2` I think it would make more sense... – MadProgrammer Oct 03 '13 at 05:28
  • 1
    Do you know Javascript? read [What does = +_ mean in JavaScript](http://stackoverflow.com/questions/15129137/what-does-mean-in-javascript) `=+` is interesting in JavaScript. – Grijesh Chauhan Oct 03 '13 at 05:30
  • 1
    -(-2) is equivalent to +2. For more info you can look here: http://en.wikipedia.org/wiki/Arithmetic – Red Alert Oct 03 '13 at 05:34
  • Close vote...?? And i don't understand why..!! – Gokul Nath KP Oct 03 '13 at 05:35
  • What's going on here?! People are *upvoting* wrong answers, *downvoting* the right answers. – Rahul Oct 03 '13 at 05:41
  • It would be easy to asume that using `=+` is a mistake really wanting to use `+=`. Maybe that's why the downvotes. – madth3 Oct 03 '13 at 06:14
  • This is actually a duplicate of: http://stackoverflow.com/questions/1642028 http://stackoverflow.com/questions/21029174 http://stackoverflow.com/questions/8825840 http://stackoverflow.com/questions/12313423 http://stackoverflow.com/questions/19151388 http://stackoverflow.com/questions/15129137 http://stackoverflow.com/questions/13946938 http://stackoverflow.com/questions/13874179 – nawfal Jan 10 '14 at 13:30
  • http://stackoverflow.com/questions/1839006 http://stackoverflow.com/questions/2034848 http://stackoverflow.com/questions/12442916 http://stackoverflow.com/questions/9955885 http://stackoverflow.com/questions/7967066 http://stackoverflow.com/questions/2939023 http://stackoverflow.com/questions/3543669 http://stackoverflow.com/questions/20754582 :) – nawfal Jan 10 '14 at 13:32

6 Answers6

11
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

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
5

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.

devnull
  • 118,548
  • 33
  • 236
  • 227
3

You are setting i equal to +2, which is what you got. What kind of output are you expecting?

borrrden
  • 33,256
  • 8
  • 74
  • 109
Red Alert
  • 3,786
  • 2
  • 17
  • 24
3

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".

1

Refer to the following image for unary operators.

enter image description here

Here is an exmaple to understand it.

public class UnaryDemo {
    public static void main(String[] args) {
        int x = 10;
        int y = 20;

        int result = +x;

        System.out.println("+x = " + result);

        result = -y;

        System.out.println("-y = " + result);
        }
    }

and the output is

+x = 10
-y = -20

So think the operator as variable = +value rather than variable =+ values. yeah That space makes it more readable.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
1

As all others are answered, I want to give the JLS reference.
Answer to your Edit

int i =- -2;

As specified in jls

  • Unary numeric promotion (§5.6.1) is performed on the operand.
  • The type of the unary minus expression is the promoted type of the operand.
  • At run time, the value of the unary minus expression is the arithmetic negation of the promoted value of the operand.

So,

System.out.println(i); //prints 2  

For integer values, negation is the same as subtraction from zero.


Note

For floating-point values, negation is not the same as subtraction from zero, because if x is +0.0, then 0.0-x is +0.0, but -x is -0.0.

Unary minus merely inverts the sign of a floating-point number. Special cases of interest:

  • If the operand is NaN, the result is NaN. (Recall that NaN has no sign (§4.2.3).)

  • If the operand is an infinity, the result is the infinity of opposite sign.

  • If the operand is a zero, the result is the zero of opposite sign.

Useful links

  1. unary operators
  2. Unary Numeric Promotion
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90