? Java是一种强类型得语言,严格要求变量要符合规定,所有变量都必须先定义再使用
java得数据类型分为两大类
值得注意得是String并不是一个数据类型,它是一个类
public class Demo3 {
public static void main(String[] args) {
int i = 128;
byte b = (byte) i; //内存溢出
//强制转换 (类型)变量名 高-->抵
//自动转换 抵-->高
System.out.println(i);
System.out.println(b);
/*
注意点:
1.不能对布尔值进行转换
2.不能把对象类型转换为不相干得类型
3.在把高容量转换到低容量得时候,强制转换
4.转换得时候可能存在内存溢出,或者精度问题
*/
}
}
由于java是强类型得语言,在有时候需要进行类型得转换
public class Demo4 {
public static void main(String[] args) {
int money = 10_0000_0000;
int years = 20;
int total = money*years;//-1474836480 溢出了
long total2 = money*years; //这个地方一样会溢出,因为在赋值之前就溢出溢出了,传给total得值也是溢出得值
long total3 = money*((long)years);
System.out.println(total);
System.out.println(total2);
System.out.println(total3);
}
}
原文:https://www.cnblogs.com/Avirus/p/15046628.html