java的基本数据类型
1.基本类型之间的转换
char类型不能和其他任何基本类型自动转换 byte char short int long float double boolean
byte<(char = short)<int<long<float<double char:无符号的整数。
2.必须强转的情况
容量小的类型自动转换为容量的类型。
byte b= 10; char ch = (char)b;//byte----->int------>char
char是无符号整型,所以范围比short范围大。
short sh = 10; char ch2 = (char)sh
byte,short,char在计算时首先转换为int,大容量(int)转换为小容量的类型(byte)时要加强制转换符。
byte b2 = 10; byte b3 =10; byte b4 =(byte)(b2+b3);
3.拆箱和装箱
装箱就是讲java的基本类型转换成对应的包装类。
拆箱就是将包装类转换成对应的基本类型。
自动装箱
自动装箱就是调用valuetOf()方法将原始类型转换成对象。
Integer a2 = Integer.valueOf(5);
自动拆箱
自动拆箱就是调用intValue()将Integer转换成对应的基本类型。
int a3 = a2.intValue();
显式装箱
Integer i2 = (Integer)10;
显式拆箱
int a4 = (int)a3