• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Numberoverflow warning

 
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ryan, in future please BeForthrightWhenCrossPostingToOtherSites.
 
Ryan McClain
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies everyone. It helped me out.
 
Legend has it that if you rub the right tiny ad, a genie comes out.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic