0

I'm sure it's not strange, rather I didn't understand the docs, but why does:

long myLong = 3600*24*365*70;
System.out.println(String.valueOf(myLong));

display -2087447296 when that is clearly not the result of 3600*24*365*70?

TimSim
  • 3,936
  • 7
  • 46
  • 83
  • 1
    this might help http://stackoverflow.com/questions/6332392/multiplication-operation-in-java-is-resulting-in-negative-value – hece Sep 02 '14 at 06:10
  • 1
    Integer overflow happening there. Make any one of them to long. That saves you from stop happening integer multiplication. – Suresh Atta Sep 02 '14 at 06:11

1 Answers1

4

try

long myLong = 3600L*24*365*70;

as your code is multiplying as int and then converting to a Long. The value as an int is overflowing the max value for an int, but will be OK as a long.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64