(1)boolean类型不能与其他类型进行转换
(2)转换规则:
byte,short,char之间不会发生转换,他们三者进行计算时首先转换成int (包括byte+byte)
byte b1 = 1;
byte b2 = 2;
byte b = b1+b2; //b1+b2 计算时都转换成int int到byte要加强制转换符 byte b =(byte)( b1+b2)
float f1 = 1.2;// 默认double型 所以改为1.2f
long l1 = 123;
long l2 = 300000000000; //默认为int 超过了int的取值范围,所以要在后边加L
int i = 1;
i = i/10;
i = i*0.1;// 0.1为double型 i*0.1 为double型
char c1='a';
char c2 = 125;
char c = c1+c2-1;// c2 = 125没问题, c1+c2为int型 要强制转换
byte b1 = 67;
byte b2 = 89;
byte b = (byte)( b1+b2); //正确 但是结果为-100,int->byte 直接卡掉前三个字节
double d=1e200;
float f = (float)d;//会溢出,不像int->byte 那样卡掉。
(2)转换规则:
- 容量小的自动转换成容量大的,容量大小排序如下:
byte,short,char之间不会发生转换,他们三者进行计算时首先转换成int (包括byte+byte)
- 容量大的转换成容量小的要进行强制转换符。
- 实数常量(12.3)默认为double
- 整数常量(123)默认为int
byte b1 = 1;
byte b2 = 2;
byte b = b1+b2; //b1+b2 计算时都转换成int int到byte要加强制转换符 byte b =(byte)( b1+b2)
float f1 = 1.2;// 默认double型 所以改为1.2f
long l1 = 123;
long l2 = 300000000000; //默认为int 超过了int的取值范围,所以要在后边加L
int i = 1;
i = i/10;
i = i*0.1;// 0.1为double型 i*0.1 为double型
char c1='a';
char c2 = 125;
char c = c1+c2-1;// c2 = 125没问题, c1+c2为int型 要强制转换
byte b1 = 67;
byte b2 = 89;
byte b = (byte)( b1+b2); //正确 但是结果为-100,int->byte 直接卡掉前三个字节
double d=1e200;
float f = (float)d;//会溢出,不像int->byte 那样卡掉。