类型转换
- Java是强类型语言,在进行有些运算时,需要用到类型转换
低----------------------->--------------------------->高
byte, short, char --> int --> long --> float --> double
- 运算中,不同类型的数据先转化为同一类型,再进行运算
- 高容量转换为低容量需要进行强制转换,低容量转换高容量自动转换
- 数据类型转换时,有一些特殊的类型会出现精度丢失或内存溢出问题
public static void main(String[] args) {
int i = 128;
byte j = (byte) i;
System.out.println(i);
System.out.println(j);
System.out.println((int) 23.7);
System.out.println((int) -45.89f);
char c = 'a';
int d = c + 1;
System.out.println(d);
System.out.println((char) d);
int money = 10_0000_0000;
int years = 20;
int totalMoney = money * years;
System.out.println(totalMoney);
long lTotalMoney = money * ((long) years);
System.out.println(lTotalMoney);
}