Win a copy of Functional Design and Architecture this week in the Functional programming forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Have I gone crazy?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm running this:


and Eclipse console gives answer as 1471228928
But my calculator says the answer is 31536000000
I thought I might have exceeded the max long value. But Oracle says:

The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).


Obviously I am doing something very, very stupid. But what?
 
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your variable is a long, but the expression is not because the values in the expression are not. You can force the expression to be evaluated as long by making any one of the values a long literal (rather than the current int literals).
 
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you're not doing anything wrong. It's just that you're not aware of how Java determines the type of literal numbers..

The key insight is that "365*24*60*60*1000" is not a long expression, but an int expression, since all its constituent parts are ints - which overflows. You can force it to be a long expression by making one of its factors a long, e.g. the first one: "365l*24*60*60*1000". The "l" after 365 basically says to the compiler: never mind what this number looks like (and it looks like an int), treat it as a long.

Lean back and observe the difference :-)
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:by making one of its factors a long, e.g. the first one: "365l*24*60*60*1000". The "l" after 365 basically says to the compiler: never mind what this number looks like (and it looks like an int), treat it as a long.



Although I'd recommend a capital 'L', which doesn't look so much like the number 1 as lowercase 'l' does.
 
Ed Dablin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all! Marked as resolved
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic