-1

Can someone explain to me why the result is 30?

System.out.println(30 - 12 / (2*5) +1);
Makoto
  • 104,088
  • 27
  • 192
  • 230
Ittiunited
  • 69
  • 1
  • 6

1 Answers1

3

This is simple arithmetic order of operations. 30 - 12 / (2 * 5) + 1 breaks down thus:

  • Work the parentheses first: 2 * 5 becomes 10. You now have 30 - 12 / 10 + 1.
  • Division takes precedence over all other present operators, so you're dividing 12/10 as integers, so you'll get 1 as the quotient. You now have 30 - 1 + 1.
  • You then subtract 30 from 1, then add 1 back to get 30.
Makoto
  • 104,088
  • 27
  • 192
  • 230