user2393012 February 2016

Why "Numeric overflow in expression" warning occurs

Using intellij 15.0.3 + Java 8u65...

lower = System.currentTimeMillis();
long upper = lower + 31536000000L; //add a year-ish

Works fine. But if I do:

lower = System.currentTimeMillis();
long upper = lower + (1000L*60*60*24*365); 

Intellij now gives a warning "Numeric overflow in expression". I'd understand if this were in fact true, and it was consistently warning over both expressions, but it's not.

Anyone know why the 2nd expression generates the warning? I'd rather have the breakdown this way than a number because it's easier for other devs on the project to understand what it's doing (though I suppose I could comment). Code still compiles obviously but I find warnings in my builds like an itch that I can't scratch.

EDIT Thanks for responses... I think this is just a caching issue in Intellij... If I know copy/paste the above I don't get the warning. If I try to edit it after the paste 1 or 2 times out of 10 I get the warning popping in.

Answers


EJP February 2016

Because the product of 1000 and 60 and 60 and 24 and 365 is 31536000000, or 0x757B12C00, which is longer than a long. When you write it as 31536000000L, it is no longer an expression, so you don't get the warning about numeric expression overflow. You should probably get some other warning, and in fact you don't actually state that you didn't ...


Steve Bosman February 2016

I have just tried this on my machine. Same version of intellij, but a slightly newer java 1.8.0_66. If I copy paste your code and assume lower is a long, I don't get the warning. If I remove the "L" I get the warning (obviously). If I put the "L" back the warning doesn't go away. If I close and reopen IntelliJ the warning goes away.

Added to the issue tracker: IDEA-151378

Post Status

Asked in February 2016
Viewed 1,918 times
Voted 10
Answered 2 times

Search




Leave an answer