As Ramya said , if negate performs abs() then compiler error should result. Ramya didn't say compiler error. It's possible a run-time exception was intended here. However, has anyone tried replacing
with
to see what happens? I believe your guesses about its behavior are incorrect.
I would say that throwing an AritmeticException would have been a good, reasonable action for the JVM to take here. But that's not what Sun chose to implement. It's really an example of a much larger problem: silent overflow (or underflow). Consider:
This prints -67153019, which is obviously not the answer most people would expect. The problem is that the correct answer 121932631112635269 is implicitly cast to an int (because someone thought it made sense that an int times and int should result in another int), and since it's too big for an int, the higher bits are simply removed. I think it would be much better if an error were thrown here, but Sun chose to do otherwise. I believe this was a poor decision which can easily create bugs. But it's unlikely to change at this point.