overflow : Java Glossary


Overflow occurs when the result of an integer multiply, add or subtract cannot fit in 32 bits. Java just quietly drops the high order bits. There is no exception. If you need to detect overflow, you would use long operands and examine the high order 32 bits of the result. If there was no overflow, they would be all 0 or all 1. To detect overflow from two longs would require testing the range of the operands before the operation and/or testing the sign of the result. Why does Java ignore overflow? Most computer hardware has no ability to automatically generate an interrupt on overflow. And some hardware has no ability to detect it. Java would have to explicitly test for it on every add, subtract and multiply, greatly slowing down production. Further, ex-C programmers are very used to this cavalier ignoring of overflow and commonly write code presuming that high order bits will be quietly dropped.

The Pentium has hardware overflow detect but no hardware interrupt. So if Java were to support overflow detect, inside the JVM (Java Virtual Machine) implementation would need to add a JO (Jump on Overflow) instruction after every add and subtract and special code to look at the high order bits of the 32x32->64-bit multiply. 64/32->32-bit division might need special handling.

This page is posted
on the web at:


Optional Replicator mirror
of mindprod.com
on local hard disk J:

Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number