下面这些是可以直接转的,转换只能按照箭头顺序转,反之不可用转会丢失数据,
这是因为byte占1个字节,short占2个字节,int占4个字节,long占8个字节,float占4个字节,double占8个字节,反过来存的话,没有那么多容量存会被丢弃
byte->short->int->long
char->int->long
float->double
下面这些是可以转换的,但是有可能会丢失精度,因为float和double是浮点型类型
int->float
int->double
long->double
byte、short和int在计算的时候会自动转换为int值来计算
所以下面会有问题:
short i = 2
i = i + 1
这里会报错,因为计算的时候i+1会自动转换为int值来计算,计算出来的是int,但是int不能存入short,如果要不报错,就使用int来接或者强制转换为short