Reputation: 1733
This is more of a theoretical question to understand Java's evaluation of arithmetic operations. Since +
and -
have the same precedence, I don't quite understand how Java evaluates the following expressions (where there are more than one +
and -
operators between the two operands).
public static void main(String[] args) {
int a = 1;
int b = 2;
System.out.println(a+-b); // results in -1
System.out.println(a-+b); // results in -1
System.out.println(a+-+b); // results in -1
System.out.println(a-+-b); // results in 3
System.out.println(a-+-+b); // results in 3
System.out.println(a+-+-b); // results in 3
System.out.println(a-+-+-b); // results in -1
System.out.println(a+-+-+b); // results in 3
}
From the Java 8 Language Specification (§15.8.2):
The binary + operator performs addition when applied to two operands of numeric type, producing the sum of the operands.
The binary - operator performs subtraction, producing the difference of two numeric operands.
[...]
Addition is a commutative operation if the operand expressions have no side effects.
Integer addition is associative when the operands are all of the same type.
What I also noticed, is that everytime the #operators is even, the result is the same and the order doesn't matter.
But when the #operators is odd, this doesn't necessarily influence the result. E.g. in the following two expressions there is one more -
than +
, however the result is different.
System.out.println(a-+-b); // results in 3
System.out.println(a-+-+-b); // results in -1
With all that information I still don't see the pattern or the way how this works.
Upvotes: 9
Views: 460
Reputation: 647
Its evaluated as it is in how you do calculations in math with addition and subtraction.
In your example all your calculations start with the smaller number a=1
, then you have -
, and +
signs between a small number and a bigger number b=2. for example when you do 1 -- 2
, you are doing 1+2
because 2 -
signs cancel each other becomes 1 + 2
, 1 - 2
will be -1 because 2 ( larger number has a - sign, which makes it a negative integer, adding a 1 to a negative integer increases its value by one
Upvotes: 2
Reputation: 124666
In math, how would you evaluate this?
a - + - b
Adding some brackets helps:
a - (+ (-b))
We can do this, because this doesn't violate the rules of precedence.
Then we can start reducing: + (-b)
is really -b
, and a - -b
is really a + b
, so the result is 1 + 2 = 3
.
Let's see the second one:
a - + - + - b
a - (+ (- (+ (-b))))
a - (+ (- (-b)))
a - (+ b)
a - b
1 - 2 = -1
So simple rules of math work naturally.
Upvotes: 9
Reputation: 27
This appears to be a mathematical issue... (- & - = +), (- & + = -) and (+ & + = +)
int a = 1;
int b = 2;
System.out.println(a+-b); // 1 +(-2) = 1-2 = -1
System.out.println(a-+b); // 1-(+2) = 1-2 = -1
System.out.println(a+-+b); // 1+-(+2) = 1-(+2) = 1-2 = 1
System.out.println(a-+-b); // 1- + (-2) = 1- (-2) = 1+2 =3
System.out.println(a-+-+b); // 1 - + - (+2) = 1 - - (+2) = 1-2 = -1
System.out.println(a+-+-b); // 1+ - + (-2) =1 - + (-2) = 1+2 = 3
System.out.println(a-+-+-b); // 1-+-+(-2) = 1 - - (-2) = 1 + (-2) = -1
System.out.println(a+-+-+b); // 1 +- +- (+2) = 1 -- (+2) = 1+2 = 3
Upvotes: 2
Reputation: 4122
I believe that the -
sign negate expressions:
int a = 1;
int b = 2;
System.out.println(a+(-b));
System.out.println(a+-b);
both print -1
. This is from oracle docs:
Unary minus operator; negates an expression
the minus simply changes the sign of the number after it. If b
were negative, then a+-b
would return 3
.
Upvotes: 2