chapter1:
* 进行混合运算的时候,byte,short,char不会相互转换,都会自动类型提升为int类型,其他类型进行混合运算的是小的数据类型提升为大的
1、byte、short、int、char 这四个数据类型在左运算的时候默认会隐身的转换成int类型的数据再进行计算,返回结果也是int类型的;
例如:
byte a = 1;
byte b = 2;
byte c;
c = (a + b);//报错。正确的方式是: c = (byte) (a + b);
但是:
//写法一、不报错,因为 a+=1 等价于 a = (short)a + 1;
short a =1; a+=1;
//写法二、报错
short s = 1; s = s + 1;
chapter2:
float和long 类型对比:
1、float以及long 类型数据底层的存储结构不同;
2、float表示的数据范围比long的要大;
long:2^63-1
float:3.4*10^38 > 2*10^38 > 2*8^38 > 2*2^3^38 = 2*2^114 > 2^63-1
float类型数据和long类型数据之间的转换:
//long转换成float是隐式转换
float f = 12.34f;
long x = 1234;
f = x;
System.out.println(f); //输出结果是:1234.0
//float 转换成 long,会损失精度,必须强制转换
float f = 12.34f;
long x = 1234;
// x = f;//float转换成long必须强制转换,否则会报错;因为float-> long会损失精度
x = (long) f;
System.out.println(x);//输出结果是:12