Quick revision
The short answer
Java's int arithmetic wraps around silently when a result exceeds 2,147,483,647, so a large price times a large quantity can come out negative, with no exception. One fix is Math.multiplyExact(price, quantity), which throws ArithmeticException on overflow; the other is to widen one operand before multiplying, (long) price * quantity, and then validate the range. Widening after the multiplication, (long) (price * quantity), does not help, because the overflow has already happened.
What to remember
In this answer, we saw that int multiplication wraps silently in two's complement, so 50,000 times 50,000 becomes -1,794,967,296. Math.multiplyExact turns the overflow into an ArithmeticException, and casting an operand to long before the multiplication computes the product in 64 bits, after which we still validate the range. Casting the result, (long) (price * quantity), is too late, because the overflow has already happened. The Math class offers a whole family of exact methods for this purpose.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Java's int arithmetic wraps around silently when a result exceeds 2,147,483,647, so a large price times a large quantity can come out negative, with no exception. One fix is Math.multiplyExact(price, quantity), which throws ArithmeticException on overflow; the other is to widen one operand before multiplying, (long) price * quantity, and then validate the range. Widening after the multiplication, (long) (price * quantity), does not help, because the overflow has already happened.
Why this question is asked
A total that turns negative is one of those bugs that looks impossible until we remember how fixed-size integers work. It appears in order totals, byte counts, timeouts in milliseconds and anything else where two reasonable numbers are multiplied. Interviewers ask it because the explanation requires two's complement, and because the two fixes reflect a real design choice: fail loudly, or compute in a bigger type. In this answer, we will recap the range of int and how wrap-around works, reproduce the negative total, apply the two fixes, look at the common mistake of casting too late, and see what else the Math class offers.
1A Quick Recap: int Range and Wrap-Around
An int is 32 bits in two's complement, so it holds values from -2,147,483,648 to 2,147,483,647. When an arithmetic result needs more than 32 bits, Java keeps only the low 32 bits and drops the rest. This is called overflow, and Java does not detect it: there is no exception and no flag. The language chose this because it matches what processors do, and checking every operation would make all arithmetic slower.
The visible effect is wrap-around. Adding 1 to the maximum value gives the minimum value, and a product that slightly exceeds the maximum becomes a large negative number.
2Reproducing the Negative Total
Suppose prices are in cents. An item costing 500 dollars is 50,000 cents, and a bulk order of 50,000 units is not unusual for a wholesale system.
- price * quantityboth int, so the multiplication is done in 32 bits
- Does the true result fit in int?YesCorrect totalNoLow 32 bits kept, often negative
- Result stored in totalno exception, no warning
3 steps in order. Play them, or step through them one at a time.
The mathematical result is 2.5 billion, which is above the int maximum. Java keeps the low 32 bits, and in two's complement those bits mean 2,500,000,000 - 2^32. Small orders never get near the limit, which is why the method only sometimes returns a negative number.
3Fix 1: Fail Loudly With Math.multiplyExact
Math.multiplyExact computes the same product, but checks for overflow and throws ArithmeticException when the result does not fit. This is the right fix when an overflowing total means the input is wrong and the request should be rejected.
The JIT compiles these methods into the plain multiply instruction followed by an overflow check, so the cost is very small. Turning the exception into a clear business error keeps it from surfacing as a generic 500 error.
4Fix 2: Widen Before Multiplying
If such totals are legitimate, the arithmetic needs a bigger type. Casting one operand to long before the multiplication makes Java promote the other operand too, so the whole product is computed in 64 bits.
Notice the difference in the last line. The parentheses make Java compute price * quantity first, in int, so the overflow has already happened, and only the wrong result is widened to long. The cast must apply to an operand, not to the product.
Widening moves the limit to about 9.2 x 10^18, which is enough for nearly every business total, but long can overflow too. If the values come from users or other systems, we still validate the range, or use Math.multiplyExact on longs. For money that needs fractions or tax, BigDecimal is the safer type.
5The Rest of the Exact Family
Math has a whole family of exact methods: addExact, subtractExact, multiplyExact, incrementExact, decrementExact and negateExact for int and long, and toIntExact for narrowing a long safely. Recent JDKs added more, such as absExact in Java 15 and floorDivExact and ceilDivExact in Java 18; check your version for the full list. They all follow the same contract: return the exact result or throw ArithmeticException.
A useful rule is to use them wherever external input feeds arithmetic. Internal counters with known bounds can use plain operators, but quantities from a request, sizes from a file or offsets from a client should go through the exact methods or an explicit range check.
6Follow-up questions
Why is Math.abs(Integer.MIN_VALUE) negative, and what does Math.absExact do?
Integer.MIN_VALUE is -2,147,483,648, and its positive counterpart, 2,147,483,648, is one more than the int maximum. Negating it wraps back to itself, so Math.abs(Integer.MIN_VALUE) returns -2,147,483,648, a negative absolute value. Math.absExact, added in Java 15, throws ArithmeticException in that one case instead. This matters in code such as Math.abs(key.hashCode()) % buckets, which can produce a negative index.
Which fix would you choose for an order total?
I would compute in long, because a large order is a legitimate business case, and then check the total against a business limit with a clear error. If the domain had a hard rule that totals must fit in int, for example because of a downstream system, I would use multiplyExact and reject the order explicitly.
Does Java ever detect integer overflow automatically?
No, not for int and long operators. Only the Math.*Exact methods, BigInteger and BigDecimal give us checked or unbounded arithmetic. Integer division by zero is the only integer arithmetic error that throws on its own.
7Conclusion
In this answer, we saw that int multiplication wraps silently in two's complement, so 50,000 times 50,000 becomes -1,794,967,296. Math.multiplyExact turns the overflow into an ArithmeticException, and casting an operand to long before the multiplication computes the product in 64 bits, after which we still validate the range. Casting the result, (long) (price * quantity), is too late, because the overflow has already happened. The Math class offers a whole family of exact methods for this purpose.
Open with
The negative number is integer overflow. An int holds up to about 2.147 billion, and Java int arithmetic wraps silently in two's complement, with no exception. If the price is 50,000 cents and the quantity is 50,000, the real product is 2.5 billion, which does not fit, so Java keeps the low 32 bits and we get minus 1,794,967,296.
Then, if they want more
Small orders are fine, which is why it only happens sometimes. I would show two fixes. The first is Math.multiplyExact(price, quantity), which does the same multiplication but throws ArithmeticException on overflow, so I can turn it into a clear validation error instead of a wrong total. The second is to widen before multiplying: (long) price * quantity.
Casting one operand promotes the other, so the product is computed in 64 bits, and then I validate the total against a business limit. The common mistake is (long)(price * quantity): the parentheses mean the multiplication happens in int first, so the overflow has already happened and only the wrong result is widened.
Math has a family of these methods, addExact, subtractExact, incrementExact, negateExact, toIntExact, and newer ones like absExact; I would check the JDK version for the full set. For money with fractions or tax, I would use BigDecimal instead.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 15, Java 18
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A method computes int total = price * quantity; and sometimes returns a negative number....": https://scrivio-interview-library.netlify.app/java/types-and-values/4d78659738/