A Note About Arithmetic Overflow

In Fig. 4.8, line 21

total = total + grade; // add grade to total

added each grade entered by the user to the total. Even this simple statement has a potential problem—adding the integers could result in a value that’s too large to store in an int variable. This is known as arithmetic overflow and causes undefined behavior, which can lead to unintended results (http://en.wikipedia.org/wiki/Integer_overflow#Security_ramifications). Figure 2.7’s Addition program had the same issue in line 23, which calculated the sum of two int values entered by the user:

sum = number1 + number2; // add numbers, then store total in sum

The maximum and minimum values ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.