Why negative value comes for long after multiplica

2019-03-06 17:52发布

问题:

This question already has an answer here:

  • Why do these two multiplication operations give different results? 2 answers
  • Why can't I assign a 'long' a value of 4 billion? 6 answers

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?

回答1:

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