-2

Why this code in java gives negative value?

    long ttt = (60 * 60 * 1000 * 24 * 26);
    System.out.println(ttt);

Result which comes as on eclipse console -2134967296?

Anything silly I am doing, may be it crossed int range I guess?

john
  • 11,311
  • 40
  • 131
  • 251
  • You're multiplying integers and then assigning it to a long. Append `l` or `L` to a value to turn them into longs while multiplying (`long ttt = (60L * 60L * 1000L * 24L * 25L);`). – Jeroen Vannevel Jun 11 '14 at 23:15

1 Answers1

9

Because 60 * 60 * 1000 * 24 * 25 overflows in the int range.

Make one of them a long so that promotion occurs

60L * 60 * 1000 * 24 * 25
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724