小转大自动转换
大转小强制转换
/**
类型转换, 大和小是按照所占的字节数来说的;
整型: byte short int long, 其中默认的数据类型是: int
浮点型: float double, 默认的: double
字符: char
布尔: boolean
*/
public class Demo03{
public static void main(String[] args){
// 小转大 --> byte -> short -> int -> long -> 自动转换
byte a = 1;
short d = a;
int b = d;
long c = b;
// 大转小 long -> int -> short -> byte -> 强制类型转换
long e = 1L;
// 错误: 不兼容的类型: 从long转换到int可能会有损失
// int f = e;
// 强制转换
int f = (int) e;
// byte g = 12; 默认的是int;
double pi = 3.1415; // 8字节
// 错误: 不兼容的类型: 从double转换到int可能会有损失
int y = (int)pi; // 4个字节
System.out.println(y);
}
}