File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Numeric Overflow error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Numeric Overflow error" Watch "Numeric Overflow error" New topic

Numeric Overflow error

Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200

For what are the reason, we get the Numeric Overflow error. By the by what is the Numeric Overflow error.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46437
Try iterating some byte numbers. Try something like this:-
What happens is that printout reads something like

2 3 4 5 6 . . . 125 126 127 -128

For the explanation you need to read up about binary numbers and two's complement arithmetic. There is a nice little diagram in Alan Clements, the Principles of Computer Hardware Oxford University Press, 3rd edition, (2000) page 179, showing the numbers going round in a circle.
You started of with 1, which is 00000001 in binary (byte), then print 00000010 (2), and keep going until you get to 127, which is the maximum value of byte, and is 01111111 in binary.

Then you go one more, getting you to binary 10000000, but in two's complement that comes to -128, so you stop your iteration.

If you tried it in a language like C# where bytes are usually unsigned, it would go from 2 3 to 255 256 0, then stop, but I think C# throws an exception at this point.

Overflow is what you call it when you come full circle over the top, and go from the maximum possible value for your number to its minimum possible value.

It is one of the two peculiar rules of integer arithmetic on computers, the other one being that 7/2*2 comes out as 6. Now that's a different story
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46437
. . . and if you used - instead of + it would go round the other way, form -128 to +127, which is called (would you believe?) underflow.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Notice how overflowing a byte didn't cause a runtime exception --
primitive types silently overflow and underflow. I think your
error is occurring in a different setting, like calling
ResultSet's getInt when getLong is required. You'll have to
tell us more -- very few mind readers pass through this one moose town.

There is no emoticon for what I am feeling!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The Java API does not contain a NumericOverflowError or NumericOverflowException class, so I'm not sure what would cause this error. Where did you see this error? What did you do that caused it? I mean what program did you run, how did you run it, etc.?

Please provide us more details so that we can help you.


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: Numeric Overflow error
It's not a secret anymore!