Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 925
Manish Patel Avatar asked Feb 08 '16 09:02

Manish Patel


People also ask

What do you mean by numeric overflow?

As stated by nico, numerical overflow is when computation finds a number that is too great for the limited number of bits allocated by software to store the number.

What is numeric overflow in C++?

In computer programming, an integer overflow occurs when an arithmetic operation attempts to create a numeric value that is outside of the range that can be represented with a given number of digits – either higher than the maximum or lower than the minimum representable value.

What is numeric overflow in Java?

java.sql.SQLException: Numeric Overflow This means that the numeric value that you're querying or inserting is larger than the DB field can hold. For example, trying to put 3000000000 in an Integer field while it can hold up max 2^32 (2147483647) will throw such an error.


2 Answers

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

like image 87
Steve Bosman Avatar answered Nov 09 '22 01:11

Steve Bosman


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); 
like image 2
Shubham Singh Avatar answered Nov 09 '22 00:11

Shubham Singh