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.

Manish Patel
  • 4,411
  • 4
  • 25
  • 48
  • Does the warning go away if you explicitly make each number a long rather than just the first? – JonK Feb 08 '16 at 09:43
  • @GordonM 1. Its [tag:java] not [tag:javascript] 2. `L` at the end of a number means that it is a [long](https://docs.oracle.com/javase/7/docs/api/java/lang/Long.html) type – Ferrybig Feb 08 '16 at 09:48
  • @JonK no I had tried that - but see edit, I think it's just an odd caching issue – Manish Patel Feb 08 '16 at 10:02
  • 2
    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: https://youtrack.jetbrains.com/issues/IDEA – Steve Bosman Feb 08 '16 at 10:03
  • @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. – Manish Patel Feb 08 '16 at 10:31
  • I've got no such problems in my environment, which is the same running on OSX. – Software Engineer Feb 08 '16 at 11:46

2 Answers2

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

Steve Bosman
  • 2,599
  • 1
  • 25
  • 41
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); 
Shubham Singh
  • 141
  • 2
  • 7