java数据类型的优先级及转换

优先级

同类型优先级

整数类型 < 浮点类型

低 < 高

强制转换

强制转换时需在数值前加(转换数据类型)
由高容量转换为低容量时就要强制转换,但不能溢出,否则会错误
实例:

int i =125
byte b = (byte)i

自动转换

数据类型容量由低到高时会自动转换为最高数据类型

实例:

int i =125
double b =i
    /*因为 int 类型比 double 类型容量低
    如果输出 double b 时
    之前定义的 int 类型时就会自动转换为 double 类型
    */

猜你喜欢

转载自blog.csdn.net/xin_shen_/article/details/106086842