int长度数据类型有:byte(8bits)、short(16bits)、int(32bits)、long(64bits)
float长度数据类型有:单精度(32bits float)、双精度(64bits double)
boolean类型变量的取值有:ture、false
char数据类型有:unicode字符,16位
对应的类类型:Integer、Float、Boolean、Character、Double、Short、Byte、Long
隐式类型转换(自动)
核心:从低精度向高精度转换
byte(1字节)–>short(2字节)–>int(4字节)–>long(8字节)–>float(4字节)–>double(8字节)
char(2字节)–>int(4字节)
注:
1:两个char型运算时,自动转换为int型;当char与别的类型运算时,也会先自动转换为int型的,再做其它类型的自动转换。
2:byte b = 3+4;编译通过,byte b1 = 1;byte b2 = 2;byte b3 = b1+b2;编译失败。两个byte相加自动转为int。
3:Java代码编译字节码后,byte short char boolean等基础类型都转换成int进行计算。
4;有double是double,没double是float,没float是long,没long是int
经典面试问题:
1.short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 +=1;有什么错?
对于 short s1=1;s1=s1+1 来说,在 s1+1 运算时会自动提升表达式的类型为 int, 那么将 int 赋予给 short 类型的变量 s1 会出现类型转换错误。
对于 short s1=1;s1+=1 来说 +=是 java 语言规定的运算符,java 编译器会对它 进行特殊处理,因此可以正确编译。
2.java 基本类型与引用类型的区别
基本类型保存原始值,引用类型保存的是引用值(引用值就是指对象在堆中所 处的位置/地址)