文章目录
包装类
概述
- 包装类: 英文为Wrapper
- 针对8种基本数据类型定义相应的引用类型:包装类(封装类)
有了类的特点,就可以了调用类中的方法.
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
- 包装类:Byte,Short,Integer,Long,Float,Double的父类为Number类
快捷键:双击shift
- alt+/
- alt+enter
基本类型,包装类和String类之间的转换
基本数据类型转换为包装类型
- 用构造器
int num1 = 10;
Integer in1 = new Integer(num1);
- /**
* 特别点:Boolean:看源码
* 只要传输的数据不是NULL,在忽略大小写的情况下只要是true,结果就是true
* 反之则是false
* 基本数据类型boolean默认值为false
* 包装类型Boolean默认值为NULL
* 引用类型String默认值为null
*/
package OOP.Wrapper;
import org.junit.Test;
public class WrapperTest {
/**
* 基本数据类--->包装类,调用包装类的构造器
*/
@Test
public void test1() {
int num1 = 10;
Integer in1 = new Integer(num1);
System.out.println(in1.toString());
Integer in2 = new Integer("123");
System.out.println(in2.toString());
//报异常NumberFormatException: For input string: "123abc"
// Integer in3 = new Integer("123abc");
// System.out.println(in3.toString());
/**
* 特别点:Boolean:看源码
* 只要传输的数据不是NUll,在忽略大小写的情况下只要是true,结果就是true
* 反之则是false
* 基本数据类型默认值为false
* 包装类型Boolean默认值为NULL
* 引用类型String默认值为null
*/
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean("TrUe");//true
System.out.println(b2);
Boolean b3 = new Boolean("true123");//false
System.out.println(b3);
Order order = new Order();
System.out.println(order.isMale);//false
System.out.println(order.isFemale);//null
System.out.println(new Order().s);//null
}
}
class Order {
boolean isMale;
Boolean isFemale;
String s;
}
包装类装换为基本数据类型
- 方法:调用包装类的xxxValue()方法
/**
* 包装类--->基本数据类型
* 方法:调用包装类的xxxValue()
*/
@Test
public void test2() {
Integer in1 = new Integer(12);
int i1 = in1.intValue();
System.out.println(i1 + 1);
Float f1 = new Float(12.3);
float f2 = f1.floatValue();
System.out.println(f2 + 1);
}
JDK5.0新特性:动装箱与自动拆箱
@Test
public void test3() {
// int num1 = 10;
// //基本数据类型--->包装类的对象
// method(num1);//自动装箱
//自动装箱
int num2 = 10;
Integer in1 = num2;//自动装箱,不用自己去new了
boolean b1 = true;
Boolean b2 = b1;
//自动拆箱:包装类--->基本数据类型
System.out.println(in1.toString());
int num3 = in1;//自动拆箱
}
public void method(Object obj) {
System.out.println(obj);
}
基本数据类型和包装类=>String类型
- 方式1:连接运算
int num1 = 10;
//方式1:连接运算
String str1 = num1 + "";
- 方式2:调用String重载的方法valueOf(Xxx xxx)
//基本数据类型,包装类--->String类型
@Test
public void test4() {
int num1 = 10;
//方式1:连接运算
String str1 = num1 + "";
//方式2:调用String重载的valueOf()方法
float f1 = 12.3f;
String str2 = String.valueOf(f1);
Double d1 = new Double(12.4);
String str3 = String.valueOf(d1);
System.out.println(str2);
System.out.println(str3);
}
String类型=>基本数据类型和包装类
- 调用包装类中的parseXxx()方法
//String类型--->基本数据类型,包装类
//调用包装类中的parseXxx()方法
@Test
public void test5() {
String str1 = "123";
//错误情况:无法强转,没有子父类的关系
// int num1 = (int) str1;
// Integer in1 = (Integer) str1;
//调用包装类中的parseXxx()方法
//可能会报NumberFormatException
int num2 = Integer.parseInt(str1);
System.out.println(num2 + 1);//124
String str2 = "true";
boolean b1 = Boolean.parseBoolean(str2);
System.out.println(b1);//true
}
包装类面试题
- 面试题一:
package OOP.Wrapper;
import org.junit.Test;
public class InterviewTest {
/**
* 分析:
* 基本类型计算自动升级:升级为double类型
* 结果为:1.0
*/
@Test
public void test1() {
Object o1 = true ? new Integer(1) : new Double(2.0);
System.out.println(o1);
}
@Test
public void test2() {
Object o2;
if (true) {
o2 = new Integer(1);
} else {
o2 = new Double(2.0);
}
System.out.println(o2);
}
}
面试题二:
@Test
public void test3() {
//==比较的引用类型,所以比的是地址值,
//结果为false
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i == j);//false
/**
* 分析:
* Integer中定义了一个内部类,IntegerCache
* 这个类中有一个数组Integer[]:存了-128-127范围的整数.
* 如果我们使用自动装箱的方式,给Integer赋值的范围在int内时,
* 可以直接使用数组中的元素,不用再去new了
* 目的:提高效率
* 所以128输出的结果为false
* 考察知识点
*/
Integer m = 1;
Integer n = 1;
System.out.println(m == n);//true
Integer x = 128;//相当于new了一个Integer对象
Integer y = 128;
System.out.println(x == y);//false
}