Quick revision
The short answer
Every operand in 24 * 60 * 60 * 1000 * 30 is an int literal, so Java multiplies in int and only widens the finished result to long. Thirty days is 2,592,000,000 milliseconds, more than Integer.MAX_VALUE (2,147,483,647), so the product wraps around to -1,702,967,296 and the timeout is negative, which most timeout code treats as already expired. The fix is to make the first operand a long, 24L * 60 * 60 * 1000 * 30, or better, to write Duration.ofDays(30).toMillis().
What to remember
In this answer, we saw that Java chooses the type of an arithmetic expression from its operands, so five int literals are multiplied in int and only the wrapped result is widened to long. Thirty days in milliseconds, 2,592,000,000, exceeds Integer.MAX_VALUE, becomes -1,702,967,296, and makes every timeout expire at once. Constant folding uses the same silent rules, so javac does not warn. We fix it with 24L on the first operand, or better with Duration.ofDays(30).toMillis() or a Duration property, and we catch the pattern with static analysis.
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.
Every operand in 24 * 60 * 60 * 1000 * 30 is an int literal, so Java multiplies in int and only widens the finished result to long. Thirty days is 2,592,000,000 milliseconds, more than Integer.MAX_VALUE (2,147,483,647), so the product wraps around to -1,702,967,296 and the timeout is negative, which most timeout code treats as already expired. The fix is to make the first operand a long, 24L * 60 * 60 * 1000 * 30, or better, to write Duration.ofDays(30).toMillis().
Why this question is asked
This bug is a favourite in interviews because the code looks obviously right: the variable is a long, the arithmetic is correct, and the compiler says nothing. Yet the value is wrong by billions and has the opposite sign. It tests whether we know when Java chooses the type of an expression, which matters far beyond timeouts, in byte sizes, money in cents and nanosecond calculations. In this answer, we will recap how Java picks the type of an arithmetic expression, reproduce the overflow step by step, see why the compiler does not warn, and then look at three fixes and how to stop the bug from coming back.
1A Quick Recap of Numeric Promotion
Java decides the type of an arithmetic expression from its operands, not from the variable the result is assigned to. This rule is called binary numeric promotion: if either operand is a double, the operation is done in double; otherwise if either is a float, in float; otherwise if either is a long, in long; otherwise in int. Literals such as 24 and 1000 are ints.
So in long timeoutMs = 24 * 60 * 60 * 1000 * 30, each multiplication sees two ints and is done in int. Only when the whole right-hand side is finished does Java widen the int result to long for the assignment. By then the damage is done.
An int has 32 bits and holds values from -2,147,483,648 to 2,147,483,647. When a multiplication produces something bigger, Java does not throw an exception. It keeps the lowest 32 bits of the true result, which is called overflow or wrap-around.
2Reproducing the Overflow Step by Step
Multiplication associates to the left, so Java computes the product one step at a time, from left to right. Let us follow each intermediate value.
- 24 * 60 = 1440int x int, done in int
- 1440 * 60 = 86400still int
- 86400 * 1000 = 86400000still fits in int
- 86400000 * 30true value 2592000000
- Fits in 32 bits?YesKeep the valueNoKeep the low 32 bits: -1702967296
- Widen to long-1702967296L is assigned
- Timeout already expireddeadline is in the past
7 steps in order. Play them, or step through them one at a time.
The widening to long comes last, after the int multiplication has already overflowed.
Notice that the first four steps are fine. Milliseconds in a day, 86,400,000, fits easily. Only the last multiplication by 30 crosses the limit. The true result, 2,592,000,000, is 444,516,353 more than Integer.MAX_VALUE, and wrapping keeps 2,592,000,000 - 4,294,967,296 = -1,702,967,296.
So the service sets a timeout of about minus 19.7 days. Code that computes a deadline as now plus timeout gets a moment in the past, and code that checks elapsed greater than timeout is true at once. That is why the timeouts happen almost immediately rather than after 30 days.
3Why the Compiler Does Not Warn
Because every operand is a constant, javac computes this expression at compile time. This is called constant folding: the class file contains the finished value -1702967296, not five multiplications. We might expect the compiler to notice the overflow while doing it, but the Java Language Specification says constant expressions follow exactly the same rules as runtime arithmetic, including silent wrap-around. So javac folds the value and stays quiet, and even -Xlint does not report it.
Static analysis tools can catch it. Error Prone has a check for overflow in constant expressions, and SpotBugs and Sonar have rules for int multiplication whose result is widened to long. Check your tool version for the exact rule names. These checks are cheap to enable and catch this exact pattern in many codebases.
4Fix 1: Make the First Operand a long
The smallest fix is to make the arithmetic happen in long from the very first step.
With 24L, the first multiplication is long times int, which promotes to long, and every later step stays in long. The second line also works here, because the overflowing step happens to be the last one. The third line shows why the L must go first: by the time Java reaches 1L, the int result has already wrapped. So the habit to teach is to put the L on the first operand, not anywhere.
5Fix 2: Let java.time Do the Arithmetic
The better fix is to stop multiplying units by hand. java.time.Duration describes an amount of time, and it does the unit conversion in long with overflow checks.
Duration.ofDays(30) also reads like the requirement, which removes a whole class of mistakes such as a missing factor of 60. Where we must multiply by hand, Math.multiplyExact throws ArithmeticException instead of wrapping silently. Notice that it overflowed in the example because both arguments were ints and the int version was chosen; with 86_400_000L it returns the right long.
In Spring Boot configuration, the same idea applies: declare the property as a Duration and write 30d in application.yml, and Spring converts it, so no one writes the multiplication at all.
6Follow-up questions
Would declaring the variable as double fix it?
No. The type of the variable does not change how the right-hand side is computed. double t = 24 * 60 * 60 * 1000 * 30 still multiplies in int and stores -1.702967296E9. Only the operand types decide the arithmetic.
How would you find other places with the same bug?
Enable the static analysis rules for int multiplication widened to long and constant overflow, then search the codebase for patterns like * 1000 and * 60 near long declarations. Replacing those with Duration or TimeUnit conversions both fixes and documents them. A unit test that asserts the expected millisecond value for each timeout constant also catches it.
Can the same thing happen at runtime with variables?
Yes, and it is more common. long bytes = count * size, where count and size are ints, overflows for large files in exactly the same way. The rule is identical: cast one operand to long before the multiplication, or use Math.multiplyExact to fail loudly.
7Conclusion
In this answer, we saw that Java chooses the type of an arithmetic expression from its operands, so five int literals are multiplied in int and only the wrapped result is widened to long. Thirty days in milliseconds, 2,592,000,000, exceeds Integer.MAX_VALUE, becomes -1,702,967,296, and makes every timeout expire at once. Constant folding uses the same silent rules, so javac does not warn. We fix it with 24L on the first operand, or better with Duration.ofDays(30).toMillis() or a Duration property, and we catch the pattern with static analysis.
Open with
The problem is that every operand is an int literal, so Java does the multiplication in int and only widens the final result to long when it assigns it.
Then, if they want more
The type of the target variable has no effect on how the right-hand side is computed. Working left to right, milliseconds per day is 86,400,000, which still fits, but multiplying by 30 gives 2,592,000,000, which is above Integer.MAX_VALUE of about 2.147 billion. The int keeps only the low 32 bits, so the value becomes minus 1,702,967,296.
A negative timeout means the deadline is already in the past, so everything times out immediately. The compiler does not warn because this is a constant expression, and constant folding follows the same wrap-around rules as runtime arithmetic, so the class file just contains the negative number.
To fix it, I would make the first operand a long, 24L times 60 and so on, so every step is done in long; putting the L at the end only works by luck. Better still, I would write Duration.ofDays(30).toMillis(), or in Spring declare the property as a Duration, because it reads like the requirement and avoids hand-written unit math.
And I would turn on the Error Prone or Sonar checks for constant overflow and int multiplication widened to long, so the pattern is caught elsewhere too.
About this answer
- Last updated
- 12 November 2026
- 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 constant is declared long timeoutMs = 24 * 60 * 60 * 1000 * 30; for 30 days, but...": https://scrivio-interview-library.netlify.app/java/types-and-values/3b31b3fd47/