Why "Numeric overflow in expression" warning occurs
Asked Answered
U

2

22

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.

Uxoricide answered 8/2, 2016 at 9:41 Comment(6)
Does the warning go away if you explicitly make each number a long rather than just the first?Atom
@GordonM 1. Its java not javascript 2. L at the end of a number means that it is a long typeMarlin
@Atom no I had tried that - but see edit, I think it's just an odd caching issueUxoricide
I've 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. I'd add this to the issue tracker: youtrack.jetbrains.com/issues/IDEAJacobin
@SteveBosman thanks, confirmed same behavior here. Thanks for raising the ticket - if you can put this as an answer I'll mark it as the accepted one.Uxoricide
I've got no such problems in my environment, which is the same running on OSX.Strychnine
J
10

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

Jacobin answered 8/2, 2016 at 10:51 Comment(1)
Still happening with Intellij 2016.2.5 + JVM 1.8.0_112Fairspoken
F
2

Any number written in literal is considered as Integer by default in java.

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

this is integer lateral calculating to generate value greater than Integer.MAX_VALUE. Hence, overflow in numeric expression.

Compiler expect int * int = int to avoid this warning you need to suggest compiler that answer is expected as long value.

The compiler will not give a warning if the above expression is written as:

long upper = lower + (1000L*60*60*24*365L); 
Ferdy answered 11/3, 2021 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.