Java 类型转换
java
类型转换
1、高到低 强制类型转换
2、低到高 不需要强制类型转换
3、不能转换布尔类型
4、操作大数,注意溢出问题
int money = 10_0000_0000; money值为1000000000,下划线不会识别,仅仅帮助辨识;
int years = 20;
int total = money * years;//-1474836480,计算时溢出了
long total2 = money * years;//默认是int,计算时已经溢出,转换操作在计算溢出之后
long total3 = money * ((long)years);
System.out.println(total3);//20000000000,正确结果