低—->高 byte,short,char->int->long->float->double
- 低到高,自动类型转换
- 高到低,强制类型转换
强制类型转换
int i1=1;
byte i2=3;
i2= (int) i2;
注意溢出
public class Demo1 {
public static void main(String[] args) {
//溢出问题
int money=10_0000_0000;
int year=20;
int gain1=money*year;
System.out.println(gain1);
long gain2=money*year; //默认是int,转换前就出问题了
long gain3=money*(long) year; //先把一个数变成long
System.out.println(gain2);
System.out.println(gain3);
}
}