2

I am new to Java and just want to calculate the number of mili seconds in a year, but the result is not as expected

long milisecondsInYear = 1000*60*60*24*365;
System.out.println(milisecondsInYear); // Expect 31536000000 but get 1471228928

Please advise me on this. Thank you in advance.

alex chen
  • 71
  • 1
  • 6

1 Answers1

1

Whenever you do any calculations java just assumes and calculates it as an int, even if you're saving the result in a long variable.

The actual result can't fit in the intvariable, hence you get that value.

To solve the problem you need to add an L at the end to let java know this is actually a long.

long milisecondsInYear = 1000*60*60*24*365L;
System.out.println(milisecondsInYear); // Expect 31536000000 but get 1471228928
Nikos Tzianas
  • 633
  • 1
  • 8
  • 21