包装类 Wrapper
基础数据类型与包装类
问题:既然有了基础数据类型,那为什么还需要包装类呢?
基础数据类型不是对象,导致无法参与转型,泛型,反射等过程。为了弥补这个缺陷,让基本数据类型的变量具有类中对象的特征,java提供了包装类。
包装类就是对基本数据类型进行包装(强化), 将其包装成一个类, 使用类中的方法可以对包装类对象进行更方便的操作.
包装类的特性
基本数据类型 | 对应 | 包装类 |
---|---|---|
int | –> | Integer |
char | –> | Character |
byte | –> | Byte |
short | –> | Short |
long | –> | Long |
boolean | –> | Boolean |
float | –> | Float |
double | –> | Double |
包装类的直接父类:
- Number:数值型包装类的直接父类
- Object:剩余两个的直接父类
**包装类都被 final 修饰:不能被继承
**
基本数据类型和String类型的相互转换
1.字符串转换成基本数据类型
- 通过包装类的构造器实现:
int i = new Integer(“12”); - 通过包装类的parseXxx(String s)静态方法:
Float f = Float.parseFloat(“12.1”);
2.基本数据类型转换成字符串
- 调用字符串重载的valueOf()方法:
String fstr = String.valueOf(2.34f); - 直接使用空字符串拼接:
String intStr = 5 + “”
以Integer为例:
-
int intValue() --> 将引用数据类型的Integer对象, 转换回基本数据类型[手动拆箱]
-
static Integer valueOf(int i) --> 返回一个表示指定的 int 值的 Integer 实例[手动装箱]
-
static int parseInt(String s) --> 将数字字符串转换为数字[重点掌握]
- int num = Integer.parseInt(“123”);
-
static String toString() --> 将Integer对象, 转换成字符串.
int --> String
A. 推荐: 使用字符串的+拼接
举例: --> 100 + ""
B. 不推荐: 将int --> Integer --> toString()
举例: Integer i = new Integer(100);
i.toString();
String – int
A. 推荐: 使用Integer.parseInt("123");
举例: Integer.parseInt("123");
B. 不推荐: 将字符串封装成Integer --> 调用intValue();
举例: Integer i = new Integer("123");
i.intValue();
JDK5的新特性:自动装箱 和 自动拆箱
1.自动装箱:把基本数据类型转换为对应的包装类类型
- 方法:将基本数据类型, 直接赋值给包装类对象的引用;
- 原理:底层是通过对应的valueOf和有参构造配合使用的
- 1)当数值在-128到127之间(一个字节)则直接返回底层缓存的引用
- 2)否则,在堆中new一个新的对象返回该对象的引用
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
2.自动拆箱:把包装类类型转换为对应的基本数据类型
- 方法:可以将包装类Integer对象, 直接赋值给int数据类型;
- 原理:底层会自动帮我们调用对应的方法:如 Integer 的 int intValue()
- 应用:赋值时、== < 判断时、算术运算 时 都会自动拆箱
public int intValue() {
return value;
}
3.结论: 自动拆装箱, 可以实现的效果是基本数据类型和引用数据类型的变量随意互换.
Integer i = 100;
Integer i2 = new Integer(100);
int num = i2;
注意:
- 自动拆装箱, 只针对于基本数据类型和包装类.
4.手动装箱: 手动的将基本数据类型, 包装成包装类对象
int num = 10;
Integer i = new Integer(num);
5.手动拆箱: 手动的将Integer对象转换为int整数
Integer i = new Integer(100);
int num = i.intValue();
部分包装类的Cach缓存
1.整数型包装类
- 1)使用new 方法实例化对象时,采用构造器在堆中开辟空间,存储该包装类对象;
- 2)使用自动拆箱创建对象时,【默认】调用包装类的静态方法 valueOf() 进行对象的初始化;
- 1)valueOf()会判断是否在byte存储范围内,来判断引用指向是否为底层缓存地址
- 2)valueOf()有两种参数传递类型:int \ String
Integer i = 5;
//等价于:
Interger i = Interger.valueOf(5);
1)自动装箱:代码默认执行对应包装类的valueOf()方法
static Integer valueOf()
2)自动拆箱:代码执行过程
byte byteValue() 以 byte 类型返回该 Integer 的值
2.布尔类型的包装类
1)布尔值:只认true 与 false
2)布尔类型的包装类:只要不是“true”,就是false
3)注意:
Boolean x = new Boolean(true);
Boolean y = new Boolean(true);
System.out.println(x == y); //false
Boolean x1 = true;
Boolean y1 = true;
System.out.println(x1 == y1); //ture
原理:自动装箱时使用了静态常量
- 1)被final修饰
- 2)构造器初始化:在堆中开辟空间,并初始化
public Boolean(boolean value) {
this.value = value;
}
- 3)自动装箱:valueOf()
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
- 4)其中 TRUE 与 FALSE 是静态常量:其值在方法区唯一
public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);
3.Character包装类
自动装箱:valueOf(char c)
public static Character valueOf(char c) {
if (c <= 127) { // must cache
return CharacterCache.cache[(int)c];
}
return new Character(c);
}
CharacterCache静态内部类:
- 类加载时,ASCII字符集中 0 到 127 对应字符,自动加载到堆中,且空间唯一;
private static class CharacterCache {
private CharacterCache(){}
static final Character cache[] = new Character[127 + 1];
static {
for (int i = 0; i < cache.length; i++)
cache[i] = new Character((char)i);
}
}
4.Float 与 Double 包装类:没有对应的Cach静态内部类
public static Float valueOf(float f) {
return new Float(f);
}
public static Double valueOf(double d) {
return new Double(d);
}
面试题
面试题1:给出打印输出结果(整数型包装类自动装箱原理)
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i==j); // false
System.out.println(i.equals(j)); // true
Integer a = 500; //Integer.valueOf(500)
Integer b = 500;
System.out.println(a==b); // false
System.out.println(a.equals(b)); // true
Integer aa = 127; //Integer.valueOf(127)
Integer bb = 127;
System.out.println(aa==bb); // true
System.out.println(aa.equals(bb)); // true
面试题2:如下两个题目输出结果相同吗?各是什么
三元运算符运算时:存在类型提升
- 如果都是变量,则按转换规则处理成一致的类型;
- 如果有一个变量且是int类型,则转换为int型处理;
- 如果有一个变量且是char类型,若int常量在[0~65535]之间则按char处理,否则按int型处理;
- 如果都是常量,如果一个是char,如果另一个是[0~65535]之间的整数按char处理;如果一个是char,另一个是其他,按照自动类型转换规则处理成一致的类型;
System.out.println(true ? 1 : 1.0); --> 1.0
public static void main(String[] args) {
char x = 'x';
int i = 10;
System.out.println(true? x : i); //120
System.out.println(true? 'x' : 10); //x
}
- 2)包装类会自动拆箱
Object o1 = true? new Integer(1) : new Double(2.0);
System.out.println(o1); //1.0
Object o2;
if(true)
o2 = new Integer(1);
else
o2 = new Double(2.0);
System.out.println(o2); //1
面试题3:注意虽然是传递的地址值,但是两个方法用的是同一地址下的数据,但数据本身并没有被改变。
public static void main(String[] args) {
Integer a = 100;
Integer b = 999;
swap(a,b);
System.out.println("main方法中:a="+a+",b="+b);
}
private static void swap(Integer a, Integer b) {
Integer temp = a;
a = b;
b = temp;
System.out.println("swap方法中:a="+a+",b="+b);
}
结果:
swap方法中:a=999,b=100
main方法中:a=100,b=999
面试题3:与基础数据类型进行判断运算时,包装类会自动拆箱
Integer a = 127;
int b = 127;
System.out.println(a == b); //true
Integer a1 = new Integer(127);
int b1 = 127;
System.out.println(a1 == b1); //true
Integer a2 = 128;
int b2 = 128;
System.out.println(a2 == b2); //true
Integer a3 = new Integer(128);
int b3 = 128;
System.out.println(a3 == b3); //true
面试题4:char类型包装类的存储机制;
System.out.println((int)'A'); // 65
Character i1 = 'A';
Character i2 = new Character('A');
char i3 = 'A';
System.out.println(i1 == i2); //false 地址判断
System.out.println(i1 == i3); //true
System.out.println(i2 == i3); //true
包装类注意事项:
- 地址值:new 地址值、缓存地址值;
- 自动拆箱:==、=、>、<、+
- == 对值与地址值的判断;