类型转换
-
由于Java是强类型语言,所以要进行有些运算的时候,需要用到类型转换
低------------------------------------------------------------>高
byte,short,chart--> int--> long --> float--> double
-
运算中,不同类型的数据先转化为同一类型,然后再进行运算。
-
注意点
- 不能对布尔值进行转换
- 不能把对象类型转换为不相干的类型
- 再把高容量转换到低容量的时候,强制转换
- 转换的时候可能出现内存溢出,或者精度问题
-
强制类型转换
public static void main(String[] args) { //强制转换 (类型)变量名 高-->低 int a = 56; byte b = (byte) a; System.out.println(b); }
-
自动类型转换
public static void main(String[] args) { //自动转换 低-->高 int a = 56; double b = a; System.out.println(b); }