文章目录
- 一、引入(基本数据类型弊端)
- 二、包装类
- (1) 模拟包装类的实现
- (2) 包装类(Wrapper Class)
- (3) 自动装箱、自动拆箱
- ① 自动装箱
- ② 自动拆箱
- 三、整数类型包装类细节 ☆
一、引入(基本数据类型弊端)
📜 对比引用类型,基本类型(byte、short、int、float、boolean …)有一些缺陷
✒️ 无法表示不存在的值(null
值)
✏️ 假如你开了一家🏨酒店,你想统计一周的盈利💰情况(如:星期一赚💯万、星期二亏10万 …),你会怎么做 ?
✏️ 假如用基本数据类型,您可能会如下图哪样干:
✏️ 上图:用一个可存放7个
int
类型元素的数组存放盈利额。100 是盈利100万、-10 是亏损10万元。这样可以表达出酒店一周的亏损值,但如何表达星期二没有开门呢 ❓
✏️ 用数字【0】来表达: 有歧义,数字【0】也可能表达的含义是【开门了,但一个客人都没有,一点钱都没赚,也一点钱都没有亏】
✏️ 此时基本数据类型的弊端就显现了,无法表示不存在的值(null 值)
✒️基本类型的操作不够面向对象(比如用基本类型调方法)
二、包装类
📕 Java platform provides wrapper classes for each of the primitive data types. These classes “wrap” the primitive in an object.
✏️ Java 为每一个基本数据类型提供了包装类型。包装类型是对基本数据类型的封装(把基本数据类型包装为引用对象【类】)
(1) 模拟包装类的实现
把基本数据类型 int 包装为引用类型:
/*** @author 庆医* @describe 把基本类型 int 包装为引用类型*/
public class Integer_ {// primitive 原始的(基本类型)private int primitive;public Integer_(int primitive) {this.primitive = primitive;}/*** 返回基本类型的值*/public int getPrimitive() {return primitive;}
}
使用自定义包装类型表达一周的盈利情况:
public class TestDemo {public static void main(String[] args) {/* 无法表达不存在的值 */// int[] weekMoney = {100, -10, 5, 123, -3, 12, 22};/* 使用包装类型 */Integer_[] weekMoney = {new Integer_(100),null, // 星期二没有开门new Integer_(5),new Integer_(123),new Integer_(-3),new Integer_(12),new Integer_(22),};/* 打印一周的亏损情况和开门情况 */for (int i = 0; i < weekMoney.length; i++) {if (weekMoney[i] == null) {System.out.println("周" + (i + 1) + ": 没有开门");continue;}int primitive = weekMoney[i].getPrimitive();System.out.println("周" + (i + 1) + ": " + primitive);}}
}
(2) 包装类(Wrapper Class)
✏️ Java 的java.lang
包中内置了基本类型的包装类
✏️ Java 的数字类型 (byte、short、int、long、float、double) 的包装类最终都继承自抽象类java.lang.Number
✏️ char 的包装类是 Character
✏️ boolean 的包装类是 Boolean
(3) 自动装箱、自动拆箱
① 自动装箱
✏️ 自动装箱:Java 编译器会自动调用包装类型的 valueOf
方法,把基本类型转换为相对应的包装类型
自动装箱:
public class TestDemo {public static void main(String[] args) {// Integer i = Integer.valueOf(11);Integer i = 11;// add(Integer.valueOf(22));add(22);}private static void add(Integer n) {}
}
⭐️ 整数类型(Byte、Short、Integer、Long) 的包装类的
valueOf
方法的底层会有缓存的操作(缓存常用的数字的包装类型)
② 自动拆箱
✏️ 自动拆箱:Java 编译器会自动调用包装类型的 xxxValue
方法,把包装类型转换为相对应的基本类型
public class TestDemo {public static void main(String[] args) {Integer i1 = 88;// class java.lang.IntegerSystem.out.println(i1.getClass());// int i2 = i1.intValue();int i2 = i1;// System.out.println(i1.intValue() == 88);System.out.println(i1 == 88); // output: true// 自动装箱Integer[] ints = {11, 22, 33, 44};int result = 0;for (Integer i : ints) {// if(i.intValue() % 2 == 0)if (i % 2 == 0) {// result += i.intValue();result += i;}}System.out.println(result);}
}
public class TestDemo {public static void main(String[] args) {// 自动装箱// Object n = Integer.valueOf(12);Object n = 12;}
}
三、整数类型包装类细节 ☆
🖊 包装类的判等不要使用 ==
或 !=
,而应该用 equals 方法
public class TestDemo {public static void main(String[] args) {Integer n1 = 88;Integer n2 = 88;Integer n3 = 888;Integer n4 = 888;System.out.println(n1 == n2); // true// n3 和 n4 比较的是地址值(n3 和 n4 不是同一个对象)System.out.println(n3 == n4); // falseSystem.out.println(n1.equals(n2)); // trueSystem.out.println(n3.equals(n4)); // true}
}
⭐️ 【整数类型】的包装类的
valueOf
方法不是直接创建一个包装类对象
⭐️ 会有缓存的操作(上图是 Integer 类的 valueOf 方法的底层)
public class TestDemo {public static void main(String[] args) {Integer i1 = 88;Integer i2 = Integer.valueOf(88);Integer i3 = new Integer(88);// trueSystem.out.println(i1 == i2);// falseSystem.out.println(i1 == i3);}
}
结束,如有错误,请不吝赐教!