溢出发生的悄然不觉,让人郁闷。小心才好。
程序:
/*
测试溢出问题
*/
public class TestTypeConvertError {
public static void main(String[] args) {
int money = 1000000001;
int years = 35000;
int total = money*years;
System.out.println(total);//发生了溢出
long total1 = money*years;
System.out.println(total1);
long total2 = money*(long)years;
System.out.println(total2);//正常显示
float total3 = 345f*23444444*years*88;
long total4 = 3333L*23333333*years;
System.out.println(total3);
System.out.println(total4);
}
}