1.自动转换
目的类型比原来的类型要大
两种类型是相互兼容的
byte->short
short->int
char->u=int
int->long
int->double
float->double float 4字节 单精度 double 8字节 双精度
两个数中有一个为double类型,另一个也为double类型,结果为double类型
否则,两个数中有一个是float类型,另一个也被转换为float类型
否则,如果两个操作数中有一个long类型,另一个也被转换为long类型
否则,两个数都转换为int类型
2.强制类型转换
浮点数转给整型:把小数位截掉,保留整数位
长的整型赋给短的整型:去位
int 4个字节 32位
byte 1个字节 8位
257 00000000 00000000 00000001 00000001
public class TestChange {
public static void main(String[] args){
//double adouble =3.55555;
//int aint=(int)adouble;
//System.out.println(aint);结果应为3
int aint=257;
byte abyte=(byte)aint;
System.out.println(abyte);//结果应为1
}
}