3 ms·
Java throws exceptions on overflow (and underflow), which presumably interferes with that, e.g.: int A = 3000000000; int B = 3000000000; int C = -3000000
by pmjordan 17y ago
Java throws exceptions on overflow (and underflow), which presumably interferes with that, e.g.:
int A = 3000000000;
int B = 3000000000;
int C = -3000000000;
int D;
D = A + B + C;
Depending on order of evaluation, this either throws an overflow exception or assigns the number 3 billion to D. There is a hard rule in the spec that defines what must happen here. In C or C++ there are no such exceptions, so the compiler can (and does) transform arithmetic expressions where it's advantageous. (this is obviously a trivial example which will compile down to a constant anyway)
- prodigal_erik 17y agoBut http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#29775 http://java.sun.com/docs/books/jls/second_edition/html/types... says > The built-in integer operators do not indicate overflow or underflow in any way. The only numeric operators that can throw an exception are the integer divide operator / and the integer remainder operator %, which throw an ArithmeticException if the right-hand operand is zero. Maybe it's true that subexpressions can't be reordered for other reasons, but offhand I can't find that restriction.