فهرست منبع

feat: 新增20260729课堂笔记及授课代码分析——包装类(Wrapper Classes):基本类型与包装类对应关系、装箱与拆箱、自动装箱/拆箱、包装类优势、缓存机制、在Bean类中的应用

WanJL 12 ساعت پیش
والد
کامیت
3acb09cd91

+ 2 - 0
README.md

@@ -34,6 +34,7 @@
 | 2026-07-24 | [多态 + 多态中的转型 + 内部类:多态概念与前提、多态成员访问特点、向上转型/向下转型、instanceof类型判断、多态的好处(开闭原则)、抽象类与多态结合(Animal→Dog;Person→Student/Teacher综合案例)、Usb接口多态案例(NoteBook接入鼠标/猫)、内部类概念与使用场景、成员内部类、私有内部类设计原则](./课堂笔记/01-JavaSE基础-笔记/20260724-笔记.md) |
 | 2026-07-27 | [匿名内部类 + Java API 与 String 类 + Math + 日期时间:内部类三大分类、匿名内部类四种使用方式、综合案例(USB/餐具/学生排序/计算器/动物叫声);API概念、String内存模型/不可变性/可变字符串StringBuffer&StringBuilder/文本块TextBlock JDK15+/String常用方法大全;Math数学类常用方法;日期时间类(Date/LocalDate/LocalTime/LocalDateTime/DateTimeFormatter)](./课堂笔记/01-JavaSE基础-笔记/20260727-笔记.md) |
 | 2026-07-28 | [Java 异常处理:异常概念、Throwable 体系结构、编译期/运行时异常、JVM 默认处理、try-catch 多 catch 块、异常常用方法、finally 资源关闭、throws 抛出、鲁棒性;自定义异常、throw/throws;Object 类:equals/hashCode/toString 重写、Objects 工具类;课后作业回顾 — String 分割去重/统计、LocalDate 天数差计算](./课堂笔记/01-JavaSE基础-笔记/20260728-笔记.md) |
+| 2026-07-29 | [包装类(Wrapper Classes):基本类型与包装类对应关系、装箱与拆箱、自动装箱/拆箱、包装类优势、包装类缓存机制、包装类在 Bean 类中的应用](./课堂笔记/01-JavaSE基础-笔记/20260729-笔记.md) |
 
 ## 二、授课代码
 
@@ -54,6 +55,7 @@
 | 2026-07-24 | 多态概念与前提、多态成员访问特点、向上转型/向下转型、instanceof运算符、多态的好处(开闭原则)、抽象类+多态(Animal→Dog、Person→Student/Teacher→Test03.runPerson())、接口多态(Usb→Mouse/Cat→NoteBook)、内部类概念与使用场景 | `c260724` | [`course/polymorphism`](./授课代码/c260724/src/course/polymorphism/) / [`course/internalClass`](./授课代码/c260724/src/course/internalClass/) |
 | 2026-07-27 | 匿名内部类:内部类三大分类概念回顾、匿名内部类语法格式与本质、三种使用方式(多态接收变量赋值 / 方法参数传递 / 匿名对象直接调用)、配套接口/抽象类定义(Usb / Tableware / Compute / Sort)、综合案例(NoteBook USB设备 / Person 餐具 / 学生排序比较 / Calculator计算器 / Animal动物叫声);Java API:API概念、String类(内存模型/构造方法/==与equals/常用方法大全/不可变性)、可变字符串(StringBuffer/SringBuilder)、文本块TextBlock JDK15+;Math数学类;日期时间类(Date/LocalDate/LocalTime/LocalDateTime/DateTimeFormatter) | `c260727` | [`course/anonymousInternalClass`](./授课代码/c260727/src/course/anonymousInternalClass/) / [`course/api`](./授课代码/c260727/src/course/api/) / [`course/api/string`](./授课代码/c260727/src/course/api/string/) / [`course/api/math`](./授课代码/c260727/src/course/api/math/) / [`course/api/date`](./授课代码/c260727/src/course/api/date/) / [`exercise/exercise01`](./授课代码/c260727/src/exercise/exercise01/) / [`exercise/exercise06`](./授课代码/c260727/src/exercise/exercise06/) / [`exercise/exercise07`](./授课代码/c260727/src/exercise/exercise07/) / [`homework_0724/p2_myarray`](./授课代码/c260727/src/homework_0724/p2_myarray/) |
 | 2026-07-28 | 异常处理(Demo01~Demo04 + Person+AgeOutOfBoundsException) + Object类(Demo01+Person 重写equals/hashCode/toString) + 课后作业回顾(String工具 + 日期数学) + Exercises | `c260728` | [`course/api`](./授课代码/c260728/src/course/api/) / [`course/exception`](./授课代码/c260728/src/course/exception/) / [`homework260727`](./授课代码/c260728/src/homework260727/) / [`exercise/exercise01`](./授课代码/c260728/src/exercise/exercise01/) / [`exercise/exercise07`](./授课代码/c260728/src/exercise/exercise07/) |
+| 2026-07-29 | 包装类(Wrapper Classes):装箱与拆箱、自动装箱/拆箱、包装类在 Bean 类中的应用(Person + Demo01) | `c260729` | [`course`](./授课代码/c260729/src/course/) |
 
 ## 三、课堂练习题
 

+ 30 - 0
授课代码/c260729/.gitignore

@@ -0,0 +1,30 @@
+### IntelliJ IDEA ###
+out/
+!**/src/main/**/out/
+!**/src/test/**/out/
+.kotlin
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+bin/
+!**/src/main/**/bin/
+!**/src/test/**/bin/
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store

+ 56 - 0
授课代码/c260729/src/course/Demo01.java

@@ -0,0 +1,56 @@
+package course;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Demo01
+ * @description 包装类 包装器类
+ * @create 2026/7/29
+ */
+public class Demo01 {
+    /*
+        Java中分为基本数据类型和引用数据类型
+        Java核心就是万事万物皆为对象
+        对象就一定有属性和方法。
+        但是Java中的8种基本数据类型却并没有对象、也没有属性也没有方法。
+        byte short int long float double char boolean
+        因为他们是从C/C++延续过来的,就是为了让C/C++程序员能够快速是学习和使用Java
+        但是严格来讲,基本数据类型并不符合Java的面向对象特性。
+        所以Java为了弥补,就为每个基本数据类型都设置了对应的引用数据类型,也就是包装器类。
+        byte    ----    Byte
+        short   ----    Short
+        int     ----    Integer
+        long    ----    Long
+        float   ----    Float
+        double  ----    Double
+        char    ----    Character
+        boolean ----    Boolean
+
+        包装器类是具有属性和方法的,属于引用数据类型
+
+        从 基本数据类型 --转换--> 引用数据类型  叫 装箱
+        从 引用数据类型 --转换--> 基本数据类型  叫 拆箱
+
+
+        使用包装器类,比使用基本数据类型要好在什么地方?
+        因为包装器类有属性有方法,我们可以操作这个数据。
+        都可以自动装箱和拆箱了,所以使用包装器类还是基本数据类型没有区别,
+        而且使用引用数据类型还可以使用它的属性和方法
+     */
+    public static void main(String[] args) {
+        int a=15;
+        Integer integer=new Integer(a); //装箱,但是在JDK9之后已经移除了,因为有了自动装箱
+
+        Integer i=a;
+        Integer i1=15;  //自动装箱 可以想String一样,直接赋值。
+
+        int a1 = integer.intValue();    //拆箱
+        System.out.println(a1);
+
+        int a2=i;       //自动拆箱
+        System.out.println(a2);
+
+
+    }
+
+}

+ 29 - 0
授课代码/c260729/src/course/Person.java

@@ -0,0 +1,29 @@
+package course;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Person
+ * @description
+ * @create 2026/7/29
+ */
+public class Person {
+    private String name;
+    private Integer age;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Integer getAge() {
+        return age;
+    }
+
+    public void setAge(Integer age) {
+        this.age = age;
+    }
+}

+ 147 - 0
课堂笔记/01-JavaSE基础-笔记/20260729-笔记.md

@@ -0,0 +1,147 @@
+# JavaSE 基础 — Day13 笔记
+
+> **日期**:2026-07-29  
+> **项目**:`c260729`  
+> **作者**:WanJL
+
+---
+
+## 一、包装类(Wrapper Classes)
+
+### 1.1 为什么需要包装类?
+
+Java 中分为**基本数据类型**和**引用数据类型**。Java 的核心思想是「万事万物皆为对象」,对象就一定有属性和方法。但 Java 中的 8 种基本数据类型(`byte`、`short`、`int`、`long`、`float`、`double`、`char`、`boolean`)是从 C/C++ 延续过来的,并不符合 Java 的面向对象特性——它们没有属性、也没有方法。
+
+为了解决这个问题,Java 为每个基本数据类型都设置了对应的引用数据类型,即**包装器类(Wrapper Classes)**。
+
+> 📄 来源:`Demo01.java`
+
+### 1.2 基本类型与包装类的对应关系
+
+| 基本数据类型 | 包装器类(引用数据类型) |
+|-------------|------------------------|
+| `byte` | `Byte` |
+| `short` | `Short` |
+| `int` | `Integer` |
+| `long` | `Long` |
+| `float` | `Float` |
+| `double` | `Double` |
+| `char` | `Character` |
+| `boolean` | `Boolean` |
+
+包装器类具有属性和方法,属于引用数据类型。
+
+> 📄 来源:`Demo01.java`
+
+### 1.3 装箱(Boxing)与拆箱(Unboxing)
+
+**装箱(Boxing)**:从基本数据类型 → 引用数据类型(包装类)  
+**拆箱(Unboxing)**:从引用数据类型(包装类) → 基本数据类型
+
+```java
+// 手动装箱(JDK9 前的方式,之后已标记为过时)
+Integer integer = new Integer(a); // 装箱
+
+// 自动装箱(JDK5+,推荐方式)
+Integer i = a;       // int → Integer,自动装箱
+Integer i1 = 15;     // 直接赋值,自动装箱
+
+// 手动拆箱
+int a1 = integer.intValue(); // 调用 intValue() 方法拆箱
+
+// 自动拆箱(JDK5+,推荐方式)
+int a2 = i;          // Integer → int,自动拆箱
+```
+
+> 📄 来源:`Demo01.java`
+
+### 1.4 自动装箱与拆箱的底层原理
+
+- **自动装箱**:编译器自动调用 `Integer.valueOf(int)` 方法
+- **自动拆箱**:编译器自动调用 `Integer.intValue()` 方法
+- 自动装箱/拆箱机制从 **JDK 5** 开始引入
+
+### 1.5 使用包装类的优势
+
+1. **拥有属性和方法**:可以调用字符串解析、进制转换等方法
+2. **泛型支持**:集合框架(如 `ArrayList<Integer>`)必须使用包装类,不能使用基本类型
+3. **允许 `null` 值**:包装类属于引用类型,可以为 `null`,这在数据库映射等场景中很有用
+4. **工具方法**:提供了丰富的静态工具方法(如 `Integer.parseInt()`、`Integer.valueOf()` 等)
+
+### 1.6 包装类在 Bean 类中的应用
+
+在实际开发中,Bean 类(如 `Person`)的属性通常会使用包装类而非基本类型,原因包括:
+- 对象的属性可能为 `null`(数据库字段可为空)
+- 便于与泛型集合配合使用
+- 统一使用引用数据类型,符合 OOP 风格
+
+```java
+public class Person {
+    private String name;
+    private Integer age;  // 使用 Integer 而非 int,允许 age 为 null
+    
+    // getter / setter 方法
+}
+```
+
+> 📄 来源:`Person.java`
+
+### 1.7 包装类的常用方法
+
+以 `Integer` 为例,包装类提供了一些非常实用的静态方法:
+
+| 方法 | 说明 |
+|------|------|
+| `Integer.parseInt(String s)` | 将字符串解析为 `int` |
+| `Integer.valueOf(int i)` | 将 `int` 转换为 `Integer`(自动装箱底层调用) |
+| `Integer.valueOf(String s)` | 将字符串转换为 `Integer` |
+| `int Integer.intValue()` | 将 `Integer` 转换为 `int`(自动拆箱底层调用) |
+| `Integer.MAX_VALUE` | `int` 的最大值(常量: 2147483647) |
+| `Integer.MIN_VALUE` | `int` 的最小值(常量: -2147483648) |
+
+其他包装类(`Double`、`Boolean`、`Character` 等)也有类似的方法。
+
+### 1.8 包装类的缓存机制
+
+部分包装类(`Integer`、`Byte`、`Short`、`Long`、`Character`)在自动装箱时存在**缓存机制**:
+
+- **Integer 缓存池**:默认缓存 `-128` ~ `127` 范围的整数
+- 在此范围内,自动装箱复用同一个对象(`==` 比较为 `true`)
+- 超出此范围,每次自动装箱都会新建对象
+
+```java
+Integer a = 100;  // 从缓存池获取
+Integer b = 100;  // 从缓存池获取
+System.out.println(a == b);  // true
+
+Integer c = 200;  // 超出缓存范围,新建对象
+Integer d = 200;  // 超出缓存范围,新建对象
+System.out.println(c == d);  // false
+
+// 推荐使用 equals() 比较包装类对象的值
+System.out.println(c.equals(d));  // true
+```
+
+> **注意**:包装类对象的值比较应始终使用 `equals()` 方法,不要使用 `==`。
+
+---
+
+## 二、随堂练习要点
+
+本日课程重点在于理解包装类的设计思想和使用方式:
+
+1. **概念理解**:为什么 Java 需要包装类?基本类型和包装类各有什么优劣?
+2. **装箱/拆箱操作**:手动 vs 自动,理解编译器的底层转换逻辑
+3. **实际应用**:在 Bean 类中优先使用包装类(如 `Integer` 替代 `int`),便于表达 `null` 语义
+4. **常见陷阱**:
+   - `==` 比较包装类时的缓存问题
+   - 包装类对象参与算术运算时的自动拆箱
+   - `null` 包装类拆箱会抛出 `NullPointerException`
+
+---
+
+## 三、拓展阅读
+
+- [Oracle Java 官方文档 — Autoboxing and Unboxing](https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html)
+- [Java 包装类缓存机制详解](https://www.baeldung.com/java-integer-cache)
+- 《Java 核心技术 卷I》第 3 章 — 包装器类与自动装箱