Why does:
System.out.println((3 + 7 + 10) * (1000 - 8) / (992 - 17))
print 3 and not 20?
System.out.println (2.0 + 1.0/2 + 1.0/6 + 1.0/24 + 1.0/120)
print 2.7166666666666663 and not 2.716666667?
System.out.println (2147483647 + 1)
print -2147483648 and not 2147483648?
Order of operations. Evaluate the parenthesis first to get (20) * (992) / 992 – 17. Then, multiplication and division from left to right to get 20 – 17. Finally, subtraction to get 3.
Floating point arithmetic. There are lots of questions here on Stack Overflow about floating point math, and a quick Google search will probably turn up more than you could ever need. The Wikipedia article might be a good start, though.
Overflow. The maximum value of an integer in Java is 2^31 – 1 or 2,147,483,647. When you add one to that value, it wraps around to the value you see. It has to do with the binary representation of the value using 2s compliment.