Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Numberoverflow warning  RSS feed

 
Ryan McClain
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This statement:
gives me a 'Numberoverflow warning' in my IntelliJ IDE.

What exactly makes this number overflow? What component is overflowing? Does the value at the right side evaluate as an integer and therefore overflows the maximum integer range? It does not automatically convert to a long type (since I am assigning it to a long)? I don't understand much what's happening here.
As I understand Java, the syntax is: destinationVariable = assignmentValue. Logically follows that I am assigning a value of 31.536.000.000 (31 billion) to a constant that holds a long type. That should be enough to hold the value, right? Doesn't Java automatically cast less specific types (integer) to more specific types (long, double)?

The solution is this:

Why do I explicitly have to convert that value to a long?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default numbers are int. So you are trying to assign int value to long variable. that's why this exception is coming.
 
Campbell Ritchie
Marshal
Posts: 56195
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan McClain wrote:. . . Does the value at the right side evaluate as an integer and therefore overflows the maximum integer range?
Yes
It does not automatically convert to a long type (since I am assigning it to a long)? . . .
No. All arithmetic, as you have been told, is done in ints until you have a long as one of the operands. The assignment operator has a lower precedence than all the other operators so you are not assigning the result to a long until after it has overflowed.
The solution is this:
. . .
Note that the (long) cast is applied to 365, not to the result of the arithmetic. That has to do with precedences, too.
Better solution:-
public static final long MILLISECONDS_IN_YEAR = 365L * 24 * 60 * 60 * 1000;

Please put some spaces in your code; we have suggestions here.
 
Ryan McClain
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use L as a number suffix in an arithmetic operation, then does that mean that all the numbers that follow it on the same line get converted to long types as well? Thus, 365L * 24 * 60 * 60 * 1000 actually means 365L * 24L * 60L * 60L * 1000L ?
 
Campbell Ritchie
Marshal
Posts: 56195
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Sort of. The 365L is a long, then when you multiply it by 24 the 24 undergoes promotion to a long, etc etc. The 24 is an int but it turns into a long during the execution of the arithmetic. Read about it in the Java® Language Specification, though that may be difficult to understand.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan, in future please BeForthrightWhenCrossPostingToOtherSites.
 
Ryan McClain
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies everyone. It helped me out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!