Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java precedence for multiple + and - operators

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.

like image 221
ncw Avatar asked Nov 06 '16 12:11

ncw


People also ask

Does || or && have higher precedence?

The logical-AND operator ( && ) has higher precedence than the logical-OR operator ( || ), so q && r is grouped as an operand. Since the logical operators guarantee evaluation of operands from left to right, q && r is evaluated before s-- .

Which operator in Java has the highest precedence?

In Java, parentheses() and Array subscript[] have the highest precedence in Java. For example, Addition and Subtraction have higher precedence than the Left shift and Right shift operators. Below is a table defined in which the lowest precedence operator show at the top of it.


3 Answers

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.

like image 163
janos Avatar answered Oct 08 '22 07:10

janos


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.

like image 20
ItamarG3 Avatar answered Oct 08 '22 07:10

ItamarG3


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
like image 33
Lord Thanatos Avatar answered Oct 08 '22 07:10

Lord Thanatos