This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Numeric Overflow error

 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

For what are the reason, we get the Numeric Overflow error. By the by what is the Numeric Overflow error.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
CR
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and if you used - instead of + it would go round the other way, form -128 to +127, which is called (would you believe?) underflow.
CR
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic