Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
i believe a+-b is executed as a+ (-b) . checking it out – Srinath Ganesh 3 hours ago
1  
@Sanket Makani -- and ++ are not allowed in that case because Java takes it as increment/decrement operation – ncw 3 hours ago
up vote 8 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

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
share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.