Java学习-基础06数据类型转换的拓展

数据类型转换的拓展

public class Demo06 {
    public static void main(String[] args) {
        //操作比较大的数的时候,注意溢出问题
        //JDK7新特性,数字之间可以使用下划线
        int money = 10_0000_0000;
        int years = 20;
        int total1 = money * years;
        System.out.println(total1);//-1474836480,计算的时候溢出了

        long total2 = money * years;
        System.out.println(total2);//-1474836480,还是负数,因为默认是int,在转换之前已经存在问题了

        //正确做法
        long total3 = money * (long)years;
        System.out.println(total3);//20000000000,输出正确结果

    }
}
posted on 2025-05-25 17:54  burgess0x  阅读(26)  评论(0)    收藏  举报