类型转换的溢出问题

public class Demo3 {
//类型转换的溢出问题
public static void main(String[] args) {
//操作比较大的数时,注意溢出问题
//JDK7新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
int year = 20;
int total = money*year;//-1474836480,计算的时候溢出了

long total2 = money *((long)years);//先把一个数转换为Long
System.out.println(total2);

//
}
}
posted @ 2021-04-21 12:05  杨得体  阅读(66)  评论(0)    收藏  举报