public class Demo02 {
public static void main(String[] args) {
int i =128;
byte b=(byte) i;//内存溢出。故为-128
//强制转换(类型)变量名 规则:高到低要强制转换 。 低到高
System.out.println(i);
System.out.println(b);
/*注意点
* 1.不能对布尔值进行转换
* 2.不能把对象类型转换为不相干的类型
* 3.在把高容量转换到低容量的时候,强制转换
* 4.转换的时候可能存在内存溢出,或者精度问题
* */
}
}
操作大的数字时
public class Demo03 { public static void main(String[] args) { int money =1000000000; int years =20; int total=money*years; System.out.println(total); } }
返回错误
public class Demo03 {
public static void main(String[] args) {
int money =1000000000;
int years =20;
int total=money*years;
long total2=money*years;//默认是INT,转换之前已经存在问题了
long total3=money*(long)years;//把数转化为Long类型。解决问题
System.out.println(total);//-1474836480,计算的时候溢出了
System.out.println(total2);//-1474836480
System.out.println(total3);//20000000000
}
}