1 什么是包装类
一种可以将基本数据类型数据当成对象使用的技术
2 包装类的作用
存放大量的方法,方便对基本数据的处理
3 包装类
基本数据类型 | 包装类 |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
4 包装类的定义以及类型的转换
包装类的定义
例如:
Integer it1 = new Integer(int i);
Integer it2 = new Integer(String s);
类型的转换
例如:
int -> Integer
Integer it1 = new Integer(int i);
Integer it2 = i.intValue();
Integer it3 = Integer.valueOf(int i);
5 装箱和拆箱
装箱:基本数据类型转换成包装类;
拆箱:包装类转换成基本数据类型。
//手动装箱
//基本数据类型 -> 包装类
int a = 10;
Integer i = Integer.valueOf(a);
System.out.println(i);
;
//手动拆箱
//包装类 -> 基本数据类型
Integer it1 = new Integer(5);
int b = i.intValue();
自动装箱拆箱
JDK自从1.5版本以后,就引入了自动拆装箱的语法,也就是在进行基本数据类型和对应的包装类转换时,系统将自动进行,这将大大方便程序员的代码书写
例如:
//包装类 -> 基本数据类型
Integer it1 = new Integer(5);
int a = it1;
//基本数据类型 -> 包装类
Integer it1 = 5;