一、包装类:
装箱:把基本数据类型封装成包装类型
拆箱:把包装类型变为基本数据类型
自动装箱:包装类型的变量接收基本数据类型的值时,先自动把基本数据类型装箱
自动拆箱:基本数据类型的变量接收包装类型的对象。会把对象拆箱
应用:包装类主要是使用自动装箱类型和自动拆箱进行映射,主要使用在泛型集合当中
二、常见的包装类
基本类型 | 对应的包装类 | 对应的转换函数 |
byte | Byte | byteValue |
short | Short | shortValue |
int | Integer | intValue |
long | Long | longValue |
float | Float | floatValue |
double | Double | doubleValue |
char | Character | charValue |
boolean | Boolean | booleanValue |
三、包装类的使用
1)常见的装箱方法:
public class PacKag_Test {
public static void main(String[] args) {
//将int类型数据,装箱为包装类
Integer a=new Integer(12);//手动装箱
Integer b=123;//自动装箱
Integer c=Integer.valueOf(1234);//返回一个int值给Integer
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
运行结果
12
123
1234
2)包装类的拆箱:
public class PacKag_Test {
public static void main(String[] args) {
//将int类型数据,装箱为包装类
Integer a=new Integer(12);//手动装箱
Integer b=123;//自动装箱
Integer c=Integer.valueOf(1234);//返回一个int值给Integer
int d=a.intValue();//手动拆箱
int e=b;//自动拆箱
System.out.println(d);
System.out.println(e);
}
}
运行结果:
12
123