针对八种基本数据类型定义相应的引用类型,就是包装类。
有了包装类,我们就可以将基本数据类型变成类,从而调用类的方法。
目录
八种包装类
基本数据类型 | 包装类 |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
doublt | Doublt |
boolean | Boolean |
char | Character |
其中,Byte,Short,Integer,Long,Float,Doublt 的父类均为 Number
基本数据类型与包装类的相互转换
基本数据类型 ——> 包装类:调用包装类的构造器
public class WrapperTest {
public static void main(String[] args) {
WT wt = new WT();
wt.test();
System.out.println(order.isOK);
System.out.println(order.IsOK);
}
}
class WT{
public void test(){
int num = 10;
Integer in1 = new Integer(num); // 包装类,可以调用类的方法
System.out.println(in1.toString());
Integer in2 = new Integer("123");
System.out.println(in2.toString());
// Integer in3 = new Integer("123abc"); // 报错 NumberFormatException
// System.out.println(in3.toString());
Float f1 = new Float(12.3f);
Float f2 = new Float("12.3");
System.out.println(f1);
System.out.println(f2);
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean("TrUe");
Boolean b3 = new Boolean("true123");
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
}
}
class Order{
boolean isOK;
Boolean IsOK;
}
>>> 10
123
12.3
12.3
true
true
false
false
null
包装类 ——> 基本数据类型:调用包装类的 xxxValue()
public class WrapperTest {
public static void main(String[] args) {
WT wt = new WT();
wt.test();
}
}
class WT{
public void test(){
int num = 10;
Integer in1 = new Integer(num);
int i1 = in1.intValue(); // 调用转换方法
System.out.println(i1 + 1);
}
}
>>> 11
自动装箱与自动拆箱
自动装箱:基本数据类型 ——> 包装类
自动拆箱:包装类 ——> 基本数据类型
public class WrapperTest {
public static void main(String[] args) {
WT wt = new WT();
wt.test();
}
}
class WT{
public void test(){
// 自动装箱:基本数据类型 ——> 包装类
int num = 10;
Integer in1 = num; // 自动装箱
System.out.println(in1.toString());
// 自动拆箱:包装类 ——> 基本数据类型
int num2 = in1; // 自动拆箱
System.out.println(num2 + 1);
}
}
>>> 10
11
基本数据类型、包装类与String的相互转换
基本数据类型、包装类 ——> String 类型,调用 String 重载的 valueOf()
public class WrapperTest {
public static void main(String[] args) {
WT wt = new WT();
wt.test();
}
}
class WT{
public void test(){
int num = 10;
String str = String.valueOf(num);
Double d1 = new Double(12.5);
String str2 = String.valueOf(d1);
System.out.println(str);
System.out.println(str2);
}
}
>>> 10
12.5
String 类型 ——> 基本数据类型、包装类,调用包装类中的 parseXxx(String s)
public class WrapperTest {
public static void main(String[] args) {
WT wt = new WT();
wt.test();
}
}
class WT{
public void test(){
String str1 = "123";
// 错误情况
// int num1 = (int)str1;
// Integer in1 = (Integer)str1;
int num2 = Integer.parseInt(str1);
System.out.println(num2 + 1);
}
}
>>> 124
包装类常见面试题
public class WrapperTest {
public static void main(String[] args) {
WT wt = new WT();
wt.test();
}
}
class WT{
public void test(){
Object o1 = true ? new Integer(1) : new Double(2.0);
System.out.println(o1);
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i == j);
Integer m = 1;
Integer n = 1;
System.out.println(m == n);
Integer x = 128;
Integer y = 128;
System.out.println(x == y);
}
}
>>> 1.0
false
true
false
o1 为 1.0 是因为三则运算符会自动提升为两个算式中较大的数据类型,int ——> double
Integer 内部定义了 IntegerCache 结构,IntegerCache 中定义了 Integer [ ],保存了从 -128 ~ 127 范围的整数。如果我们使用自动装箱的方式,给 Integer 赋值的范围在 -128 ~ 127 范围内时,可以直接使用数组中的元素,不用再去 new 了,提高了效率。