parse long to negative number

前端 未结 7 1565
北恋
北恋 2021-01-20 14:48

code:

public class Main{
    public static void main(String[] a){
        long t=24*1000*3600;
        System.out.println(t*25);
        System.out.println(2         


        
7条回答
  •  孤城傲影
    2021-01-20 15:27

    In the first case you are printing a long but in the second, you are printing it as int.

    And int has a range from: -2^31 to 2^31 - 1 which is just below what you are calculating (int max: 2147483647 you: 2160000000) so you overflow the int to the negative range.

    You can force the second one to use long as well:

    System.out.println(24L*1000*3600*25);
    

提交回复
热议问题