why is 24 * 60 * 60 * 1000 * 1000 divided by 24 * 60 * 60 * 1000 not equal to 1000 in Java?

前端 未结 3 1442
情书的邮戳
情书的邮戳 2020-12-06 05:26

why is 24 * 60 * 60 * 1000 * 1000 divided by 24 * 60 * 60 * 1000 not equal to 1000 in Java?

3条回答
  •  自闭症患者
    2020-12-06 06:31

    Because the multiplication overflows 32 bit integers. In 64 bits it's okay:

    public class Test
    {
        public static void main(String[] args)
        {
            int intProduct = 24 * 60 * 60 * 1000 * 1000;
            long longProduct = 24L * 60 * 60 * 1000 * 1000;
            System.out.println(intProduct); // Prints 500654080
            System.out.println(longProduct); // Prints 86400000000
       }
    }
    

    Obviously after the multiplication has overflowed, the division isn't going to "undo" that overflow.

提交回复
热议问题