2

I know that array operators have the precedence. Then the binary arthimetic operators * , / , % . Then + and - which they are low precedence.

But I'm confused which one will java solve first in this example. And if we have 2 operators have the same priority, what operator will be used first in java?

Thank you.

int x = y = -2 + 5 * 7 - 7 / 2 % 5;

If someone could solve this for me and explain to me part by part. Because this always confuses me in exams.

adarshr
  • 61,315
  • 23
  • 138
  • 167
Mohammad Fadin
  • 499
  • 5
  • 11
  • 22
  • It is based on BODMAS rule - Brackets Off Division Multiplication Addidition Subtraction in order. – Mahesh Mar 17 '11 at 11:12
  • 2
    If you are unsure about operator precedence, then just add extra parentheses. It doesn't have any disadvantages, like making your code slower, and in many cases makes the code more readable anyway. – RoToRa Mar 17 '11 at 11:14

5 Answers5

8

If operators have the same precedence then they are evaluated from left to right.

From the tutorial:

When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

In the expression, 7 / 2 % 5, the / and % have the same precedence, so going left to right 7 / 2 = 3 and 3 % 5 = 3.

The highest precedence is given to * / %. Here is the breakdown of your example:

  -2 + 5 * 7 - 7 / 2 % 5
= -2 + (5 * 7) - (7 / 2 % 5)
= -2 + 35 - (3 % 5)
= -2 + 35 - 3
= 30
dogbane
  • 266,786
  • 75
  • 396
  • 414
3

y will be assigned the value of -2 + 5 * 7 - 7 / 2 % 5. Then x will be assigned y's value.

Arithmetic expression will be evaluated like:

-2 + (5 * 7) - ((7 / 2) % 5)

Here's an explanation of Java's operator precedence.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
2

This looks like what you need to read: Java Operators Tutorial.

Read the tutorial and then write yourself an example program and play around with it until you're happy with operator precedence. It's the best way to learn.

Brian Beckett
  • 4,742
  • 6
  • 33
  • 52
1
int x = y = -2 + 5 * 7 - 7 / 2 % 5;

is same as

int x = y = (-2 + ((5 * 7) - ((7 / 2) % 5)));

/,* and % (multiplicative) are having same precedence and their association is left to right.
+ and - (additive)are having same precedence and their association is left to right.
multiplicative operations are higher precedence over additive operations.

Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
0

Not entirely related, but you may find this of interest. It is to do with sequence points, which are points in a program which are essentially the points where the compiler ensures that everything is synchronised. It arose on SO because of the question what does

x = x++; // Operator prcedence and/or sequence point problem;

do? Or worse

x[i]=i++ + 1;// sequence point problem

Why does this go into an infinite loop?

http://www.angelikalanger.com/Articles/VSJ/SequencePoints/SequencePoints.html

Community
  • 1
  • 1
Jaydee
  • 4,138
  • 1
  • 19
  • 20
  • In Java, there is not really a problem here, since left operands are always evaluated before the right operands (or at least, the program has to have the same result as if it was done this way). – Paŭlo Ebermann Mar 17 '11 at 12:32
  • Have a look at the first link "Why does this go into an infinite loop?". It is an example of Java code while(x<3) { x = x++; System.out.println(x); } which goes into an infinite loop because the pre-increment value of x gets assigned to x after x gets incremented. – Jaydee Mar 17 '11 at 12:57
  • I should have mentioned that the sequence point issue doesn't really relate to java, it is more C/C++, but the order of evaluation in Java is not always obvious. So to that extent you are correct. – Jaydee Mar 17 '11 at 13:12
  • I've read this, but I don't think it really is a case of *operator precedence* or *sequence point problem*. Order of evaluation (for binary operators) is always: 1. left operand, 2. right operand, 3. operator. The problem here is that often the `++` operator is explained wrong. – Paŭlo Ebermann Mar 17 '11 at 13:53
  • It is a case of misunderstanding the order of evaluation. The student expected x++ to be evaluated and assigned to x. What actually happens is that a copy of x is made for assignment, the original x is incremented and then the copy is assigned, overwriting the incremented value in x. Bad code, but it illustrates the problem. – Jaydee Mar 17 '11 at 15:23