Java division returns incorrect result

后端 未结 5 1831
不思量自难忘°
不思量自难忘° 2021-01-15 15:28
 public static void main(String[] args) {
        final long a= 24 * 60 * 60 * 1000 * 1000;
        final long b= 24 * 60 * 60 * 1000;

        System.out.println(a/         


        
5条回答
  •  北荒
    北荒 (楼主)
    2021-01-15 16:03

    24 * 60 * 60 * 1000 * 1000 this cause numeric overflow since it will consider as an int value. So you will get odd result. You should use long here.

    You should use 24 * 60 * 60 * 1000 * 1000L (This is how you define long)

    How to initialize long in Java?

提交回复
热议问题