Java 对象的创建与引用
一、使用 new 运算符创建对象
1.1 基本语法
类名 引用变量 = new 类名(构造参数);
// 示例
Student stu = new Student("张三", 20);
1.2 创建过程分解
<mxfile>
<diagram id="java-object-creation" name="Java对象创建解析">
<mxGraphModel dx="1148" dy="549" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="680" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#6366F1;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="620" width="800" x="50" y="30" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E1E1E;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#DCDCAA;rounded=1;fontFamily=Consolas;" value="Student stu = new Student("张三", 20);" vertex="1">
<mxGeometry height="60" width="800" x="50" y="30" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#1E40AF;rounded=1;fontFamily=Consolas;" value="Student stu" vertex="1">
<mxGeometry height="45" width="160" x="100" y="120" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#B45309;rounded=1;fontFamily=Consolas;" value="=" vertex="1">
<mxGeometry height="45" width="50" x="380" y="120" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#166534;rounded=1;fontFamily=Consolas;" value="new Student("张三", 20)" vertex="1">
<mxGeometry height="45" width="280" x="530" y="120" as="geometry" />
</mxCell>
<mxCell id="7" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#3B82F6;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="180" y="165" as="sourcePoint" />
<mxPoint x="180" y="220" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="8" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#F59E0B;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="405" y="165" as="sourcePoint" />
<mxPoint x="405" y="220" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="9" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#22C55E;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="670" y="165" as="sourcePoint" />
<mxPoint x="670" y="220" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="200" width="200" x="80" y="220" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="声明引用变量" vertex="1">
<mxGeometry height="45" width="200" x="80" y="220" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontStyle=0;fontColor=#1E40AF;whiteSpace=wrap;" value="📌 在栈内存中创建
 一个引用变量" vertex="1">
<mxGeometry height="50" width="170" x="95" y="275" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#EFF6FF;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#1E40AF;rounded=1;fontFamily=Consolas;" value="类型: Student" vertex="1">
<mxGeometry height="35" width="160" x="100" y="335" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=#EFF6FF;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#1E40AF;rounded=1;fontFamily=Consolas;" value="名称: stu" vertex="1">
<mxGeometry height="35" width="160" x="100" y="375" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="200" width="200" x="305" y="220" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="赋值运算符" vertex="1">
<mxGeometry height="45" width="200" x="305" y="220" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontStyle=0;fontColor=#B45309;whiteSpace=wrap;" value="🔗 将对象的内存地址
 赋值给引用变量" vertex="1">
<mxGeometry height="50" width="180" x="320" y="275" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF9C3;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#B45309;rounded=1;fontFamily=Consolas;" value="stu → 0x7F3A" vertex="1">
<mxGeometry height="35" width="160" x="325" y="340" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF9C3;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#92400E;rounded=1;" value="建立引用关系" vertex="1">
<mxGeometry height="30" width="160" x="325" y="380" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="200" width="280" x="530" y="220" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="创建对象" vertex="1">
<mxGeometry height="45" width="280" x="530" y="220" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=left;verticalAlign=middle;fontSize=15;fontStyle=0;fontColor=#166534;rounded=1;" value="1️⃣ 堆中分配内存空间" vertex="1">
<mxGeometry height="32" width="250" x="545" y="269" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=left;verticalAlign=middle;fontSize=15;fontStyle=0;fontColor=#166534;rounded=1;" value="2️⃣ 初始化成员属性" vertex="1">
<mxGeometry height="32" width="250" x="545" y="306" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=left;verticalAlign=middle;fontSize=15;fontStyle=0;fontColor=#166534;rounded=1;" value="3️⃣ 执行构造方法" vertex="1">
<mxGeometry height="32" width="250" x="545" y="343" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=left;verticalAlign=middle;fontSize=15;fontStyle=0;fontColor=#166534;rounded=1;" value="4️⃣ 返回对象内存地址" vertex="1">
<mxGeometry height="32" width="250" x="545" y="380" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#A855F7;strokeWidth=2;dashed=1;" value="" vertex="1">
<mxGeometry height="180" width="730" x="80" y="450" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🧠 内存模型示意" vertex="1">
<mxGeometry height="35" width="150" x="80" y="450" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="110" width="180" x="110" y="500" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="栈内存 Stack" vertex="1">
<mxGeometry height="30" width="180" x="110" y="500" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=#EFF6FF;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;rounded=1;fontFamily=Consolas;" value="stu" vertex="1">
<mxGeometry height="30" width="60" x="130" y="545" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#B45309;rounded=1;fontFamily=Consolas;" value="0x7F3A" vertex="1">
<mxGeometry height="30" width="70" x="200" y="545" as="geometry" />
</mxCell>
<mxCell id="32" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#A855F7;endFill=1;dashed=1;dashPattern=8 8;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="280" y="560" as="sourcePoint" />
<mxPoint x="400" y="560" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontStyle=0;fontColor=#7C3AED;" value="指向" vertex="1">
<mxGeometry height="20" width="50" x="315" y="535" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="110" width="390" x="400" y="500" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="堆内存 Heap" vertex="1">
<mxGeometry height="30" width="390" x="400" y="500" as="geometry" />
</mxCell>
<mxCell id="36" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#86EFAC;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="350" x="420" y="540" as="geometry" />
</mxCell>
<mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;fontFamily=Consolas;" value="0x7F3A" vertex="1">
<mxGeometry height="22" width="70" x="425" y="545" as="geometry" />
</mxCell>
<mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#166534;" value="Student 对象" vertex="1">
<mxGeometry height="22" width="90" x="500" y="545" as="geometry" />
</mxCell>
<mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontStyle=0;fontColor=#166534;rounded=1;fontFamily=Consolas;" value="name="张三"" vertex="1">
<mxGeometry height="22" width="100" x="600" y="545" as="geometry" />
</mxCell>
<mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontStyle=0;fontColor=#166534;rounded=1;fontFamily=Consolas;" value="age=20" vertex="1">
<mxGeometry height="22" width="60" x="705" y="545" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontStyle=0;fontColor=#6B7280;" value="实例属性" vertex="1">
<mxGeometry height="20" width="90" x="640" y="570" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
1.3 详细执行步骤
| 步骤 | 操作 | 说明 |
|---|---|---|
| ① | 加载类 | 如果 Student 类未加载,先加载到方法区 |
| ② | 分配内存 | 在堆中为新对象分配内存空间 |
| ③ | 默认初始化 | 属性赋默认值(0/null/false) |
| ④ | 显式初始化 | 执行属性声明时的赋值 |
| ⑤ | 构造方法 | 执行构造方法中的代码 |
| ⑥ | 返回地址 | 将对象内存地址赋给引用变量 |
1.4 代码示例:观察初始化顺序
public class Student {
// ② 显式初始化
private String name = "默认姓名";
private int age = 0;
// ③ 构造方法
public Student(String name, int age) {
System.out.println("构造前 name = " + this.name); // 输出:默认姓名
this.name = name;
this.age = age;
System.out.println("构造后 name = " + this.name); // 输出:张三
}
public static void main(String[] args) {
Student stu = new Student("张三", 20);
}
}
输出结果:
构造前 name = 默认姓名
构造后 name = 张三
二、堆内存与栈内存
2.1 JVM 内存结构概览
<mxfile>
<diagram id="jvm-runtime-memory" name="JVM运行时内存">
<mxGraphModel dx="1388" dy="663" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#6366F1;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="710" width="820" x="40" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="JVM 运行时内存" vertex="1">
<mxGeometry height="55" width="820" x="40" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#A855F7;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="120" width="760" x="70" y="95" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="方法区 (Method Area)" vertex="1">
<mxGeometry height="40" width="760" x="70" y="95" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#C084FC;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="50" width="150" x="100" y="150" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#7C3AED;fontFamily=Consolas;" value="Student.class" vertex="1">
<mxGeometry height="50" width="150" x="100" y="150" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#C084FC;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="50" width="150" x="270" y="150" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#7C3AED;fontFamily=Consolas;" value="String.class" vertex="1">
<mxGeometry height="50" width="150" x="270" y="150" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#9333EA;" value="← 类信息 / 常量池 / 静态变量" vertex="1">
<mxGeometry height="40" width="220" x="440" y="155" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="350" width="350" x="70" y="235" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="栈内存 (Stack)" vertex="1">
<mxGeometry height="40" width="350" x="70" y="235" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#60A5FA;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="160" width="300" x="95" y="290" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="main() 栈帧" vertex="1">
<mxGeometry height="32" width="300" x="95" y="290" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#93C5FD;strokeWidth=1;" value="" vertex="1">
<mxGeometry height="40" width="260" x="115" y="335" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;fontFamily=Consolas;" value="stu" vertex="1">
<mxGeometry height="40" width="50" x="125" y="335" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;" value="=" vertex="1">
<mxGeometry height="40" width="30" x="175" y="335" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#B45309;rounded=1;fontFamily=Consolas;" value="0x1234" vertex="1">
<mxGeometry height="30" width="80" x="205" y="340" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontStyle=0;fontColor=#6B7280;" value="(引用)" vertex="1">
<mxGeometry height="30" width="60" x="290" y="340" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#93C5FD;strokeWidth=1;" value="" vertex="1">
<mxGeometry height="40" width="260" x="115" y="385" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;fontFamily=Consolas;" value="age" vertex="1">
<mxGeometry height="40" width="50" x="125" y="385" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;" value="=" vertex="1">
<mxGeometry height="40" width="30" x="175" y="385" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#166534;rounded=1;fontFamily=Consolas;" value="20" vertex="1">
<mxGeometry height="30" width="80" x="205" y="390" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontStyle=0;fontColor=#6B7280;" value="(基本类型)" vertex="1">
<mxGeometry height="30" width="70" x="290" y="390" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0F9FF;strokeColor=#BAE6FD;strokeWidth=1;dashed=1;" value="" vertex="1">
<mxGeometry height="105" width="300" x="95" y="465" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=#0EA5E9;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📋 特点" vertex="1">
<mxGeometry height="25" width="70" x="95" y="465" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#1E40AF;whiteSpace=wrap;" value="• 存储局部变量和引用
• 方法结束自动释放
• 线程私有" vertex="1">
<mxGeometry height="70" width="200" x="110" y="495" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="350" width="350" x="480" y="235" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="堆内存 (Heap)" vertex="1">
<mxGeometry height="40" width="350" x="480" y="235" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#86EFAC;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="160" width="300" x="505" y="290" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Student 对象" vertex="1">
<mxGeometry height="32" width="300" x="505" y="290" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;" value="" vertex="1">
<mxGeometry height="70" width="260" x="525" y="335" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#166534;fontFamily=Consolas;" value="name = "张三"" vertex="1">
<mxGeometry height="28" width="150" x="535" y="340" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#166534;fontFamily=Consolas;" value="age = 20" vertex="1">
<mxGeometry height="28" width="150" x="517" y="370" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#B45309;rounded=1;fontFamily=Consolas;" value="地址: 0x1234" vertex="1">
<mxGeometry height="28" width="120" x="555" y="415" as="geometry" />
</mxCell>
<mxCell id="36" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#BBF7D0;strokeWidth=1;dashed=1;" value="" vertex="1">
<mxGeometry height="105" width="300" x="505" y="465" as="geometry" />
</mxCell>
<mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#16A34A;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📋 特点" vertex="1">
<mxGeometry height="25" width="70" x="505" y="465" as="geometry" />
</mxCell>
<mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#166534;whiteSpace=wrap;" value="• 存储对象实例
• GC 负责回收
• 线程共享" vertex="1">
<mxGeometry height="70" width="200" x="520" y="495" as="geometry" />
</mxCell>
<mxCell id="39" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=4;strokeColor=#F59E0B;endFill=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;exitX=1;exitY=0.5;exitDx=0;exitDy=0;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="395" y="354.5" as="sourcePoint" />
<mxPoint x="505" y="354.5" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#F59E0B;" value="引用指向" vertex="1">
<mxGeometry height="20" width="70" x="415" y="330" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;dashed=1;" value="" vertex="1">
<mxGeometry height="95" width="760" x="70" y="600" as="geometry" />
</mxCell>
<mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💡 内存分配总结" vertex="1">
<mxGeometry height="30" width="140" x="70" y="600" as="geometry" />
</mxCell>
<mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#166534;rounded=1;" value="基本类型" vertex="1">
<mxGeometry height="28" width="80" x="100" y="650" as="geometry" />
</mxCell>
<mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontStyle=0;fontColor=#92400E;" value="→ 值直接存储在栈中" vertex="1">
<mxGeometry height="28" width="160" x="190" y="650" as="geometry" />
</mxCell>
<mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="引用类型" vertex="1">
<mxGeometry height="28" width="80" x="380" y="650" as="geometry" />
</mxCell>
<mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontStyle=0;fontColor=#92400E;" value="→ 栈存引用地址,堆存对象实例" vertex="1">
<mxGeometry height="28" width="230" x="470" y="650" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
2.2 栈内存 vs 堆内存 对比表
| 特性 | 栈内存 (Stack) | 堆内存 (Heap) |
|---|---|---|
| 存储内容 | 局部变量、方法参数、引用地址 | 对象实例、数组 |
| 生命周期 | 方法执行期间 | 直到无引用被 GC 回收 |
| 分配速度 | 快(指针移动) | 慢(需找空闲空间) |
| 空间大小 | 较小(默认 1MB) | 较大(可配置 GB 级) |
| 线程关系 | 线程私有 | 线程共享 |
| 内存管理 | 自动释放 | 垃圾回收器管理 |
| 溢出异常 | StackOverflowError | OutOfMemoryError |
2.3 代码示例:理解内存分配
public class MemoryDemo {
public static void main(String[] args) {
// 基本类型 → 值存在栈中
int num = 100;
// 引用类型 → 引用在栈中,对象在堆中
Student stu = new Student("张三", 20);
// 数组也是对象 → 堆中
int[] arr = new int[]{1, 2, 3};
// 调用方法
process(stu);
}
public static void process(Student s) {
// s 是新的引用,指向同一个对象
String name = s.getName(); // name 引用在栈中
}
}
内存图解:
<mxfile>
<diagram id="stack-heap-memory-fixed" name="栈内存与堆内存">
<mxGraphModel dx="1388" dy="663" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="280" width="820" x="40" y="30" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="栈内存 (Stack)" vertex="1">
<mxGeometry height="40" width="820" x="40" y="30" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#60A5FA;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="200" width="340" x="70" y="90" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#2563EB;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="main() 栈帧" vertex="1">
<mxGeometry height="30" width="340" x="70" y="90" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#93C5FD;strokeWidth=1;" value="" vertex="1">
<mxGeometry height="40" width="300" x="90" y="140" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;fontFamily=Consolas;" value="int num = 100" vertex="1">
<mxGeometry height="40" width="150" x="100" y="140" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#F3F4F6;align=center;verticalAlign=middle;fontSize=11;fontColor=#6B7280;rounded=1;" value="(基本类型-值)" vertex="1">
<mxGeometry height="24" width="90" x="280" y="148" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#A855F7;strokeWidth=1;" value="" vertex="1">
<mxGeometry height="40" width="300" x="90" y="190" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#6B21A8;fontFamily=Consolas;" value="Student stu" vertex="1">
<mxGeometry height="40" width="140" x="100" y="190" as="geometry" />
</mxCell>
<mxCell id="10_addr" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=right;verticalAlign=middle;fontSize=12;fontColor=#9CA3AF;fontFamily=Consolas;" value="0x1234" vertex="1">
<mxGeometry height="40" width="60" x="320" y="190" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=1;" value="" vertex="1">
<mxGeometry height="40" width="300" x="90" y="240" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#92400E;fontFamily=Consolas;" value="int[] arr" vertex="1">
<mxGeometry height="40" width="140" x="100" y="240" as="geometry" />
</mxCell>
<mxCell id="12_addr" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=right;verticalAlign=middle;fontSize=12;fontColor=#9CA3AF;fontFamily=Consolas;" value="0x5678" vertex="1">
<mxGeometry height="40" width="60" x="320" y="240" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#86EFAC;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="200" width="340" x="490" y="90" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=#16A34A;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="process() 栈帧" vertex="1">
<mxGeometry height="30" width="340" x="490" y="90" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#A855F7;strokeWidth=1;" value="" vertex="1">
<mxGeometry height="40" width="300" x="510" y="140" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#6B21A8;fontFamily=Consolas;" value="Student s" vertex="1">
<mxGeometry height="40" width="130" x="520" y="140" as="geometry" />
</mxCell>
<mxCell id="16_addr" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=right;verticalAlign=middle;fontSize=12;fontColor=#9CA3AF;fontFamily=Consolas;" value="0x1234" vertex="1">
<mxGeometry height="40" width="60" x="740" y="140" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FCE7F3;strokeColor=#EC4899;strokeWidth=1;" value="" vertex="1">
<mxGeometry height="40" width="300" x="510" y="190" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#9D174D;fontFamily=Consolas;" value="String name" vertex="1">
<mxGeometry height="40" width="160" x="520" y="190" as="geometry" />
</mxCell>
<mxCell id="18_addr" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=right;verticalAlign=middle;fontSize=12;fontColor=#9CA3AF;fontFamily=Consolas;" value="0x9999" vertex="1">
<mxGeometry height="40" width="60" x="740" y="190" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="260" width="820" x="40" y="360" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="堆内存 (Heap)" vertex="1">
<mxGeometry height="40" width="820" x="40" y="360" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="160" width="180" x="70" y="420" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;fontFamily=Consolas;" value="int[] (0x5678)" vertex="1">
<mxGeometry height="30" width="180" x="70" y="420" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF9C3;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#B45309;rounded=1;fontFamily=Consolas;spacingLeft=10;" value="[0] = 1" vertex="1">
<mxGeometry height="30" width="150" x="85" y="460" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF9C3;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#B45309;rounded=1;fontFamily=Consolas;spacingLeft=10;" value="[1] = 2" vertex="1">
<mxGeometry height="30" width="150" x="85" y="500" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF9C3;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#B45309;rounded=1;fontFamily=Consolas;spacingLeft=10;" value="[2] = 3" vertex="1">
<mxGeometry height="30" width="150" x="85" y="540" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#A855F7;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="120" width="220" x="320" y="420" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;fontFamily=Consolas;" value="Student (0x1234)" vertex="1">
<mxGeometry height="30" width="220" x="320" y="420" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#FAF5FF;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#7C3AED;rounded=1;fontFamily=Consolas;spacingLeft=10;" value="name" vertex="1">
<mxGeometry height="30" width="180" x="340" y="460" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#FAF5FF;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#7C3AED;rounded=1;fontFamily=Consolas;spacingLeft=10;" value="age = 20" vertex="1">
<mxGeometry height="30" width="180" x="340" y="500" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FCE7F3;strokeColor=#EC4899;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="100" width="200" x="620" y="420" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#EC4899;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;fontFamily=Consolas;" value="String (0x9999)" vertex="1">
<mxGeometry height="30" width="200" x="620" y="420" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#FDF2F8;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#BE185D;rounded=1;fontFamily=Consolas;" value="value = "张三"" vertex="1">
<mxGeometry height="40" width="170" x="635" y="465" as="geometry" />
</mxCell>
<mxCell id="link_arr" edge="1" parent="1" source="11" style="edgeStyle=orthogonalEdgeStyle;rounded=1;curved=1;html=1;strokeWidth=3;strokeColor=#F59E0B;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="21">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="70" y="260" />
<mxPoint x="70" y="400" />
<mxPoint x="160" y="400" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="link_stu" edge="1" parent="1" source="9" style="edgeStyle=orthogonalEdgeStyle;rounded=1;curved=1;html=1;strokeWidth=3;strokeColor=#A855F7;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0.25;entryY=0;entryDx=0;entryDy=0;" target="26">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="430" y="210" />
<mxPoint x="430" y="320" />
<mxPoint x="375" y="320" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="link_s" edge="1" parent="1" source="15" style="edgeStyle=orthogonalEdgeStyle;rounded=1;curved=1;html=1;strokeWidth=3;strokeColor=#A855F7;strokeDasharray=4 4;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=0.75;entryY=0;entryDx=0;entryDy=0;" target="26">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="470" y="160" />
<mxPoint x="470" y="320" />
<mxPoint x="485" y="320" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="link_name" edge="1" parent="1" source="17" style="edgeStyle=orthogonalEdgeStyle;rounded=1;curved=1;html=1;strokeWidth=3;strokeColor=#EC4899;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0.75;entryY=0;entryDx=0;entryDy=0;" target="30">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="830" y="210" />
<mxPoint x="830" y="380" />
<mxPoint x="770" y="380" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="link_prop" edge="1" parent="1" source="28" style="edgeStyle=orthogonalEdgeStyle;rounded=1;curved=1;html=1;strokeWidth=2;strokeColor=#7C3AED;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" target="30">
<mxGeometry relative="1" as="geometry">
<mxPoint x="520" y="475" as="sourcePoint" />
<mxPoint x="600" y="475" as="targetPoint" />
</mxGeometry>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
三、对象实体与对象引用
3.1 核心概念
<mxfile>
<diagram id="object-reference-instance" name="对象引用与对象实体">
<mxGraphModel dx="900" dy="600" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="600">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=3;" vertex="1" parent="1">
<mxGeometry x="40" y="30" width="820" height="540" as="geometry" />
</mxCell>
<mxCell id="3" value="对象引用" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#3B82F6;" vertex="1" parent="1">
<mxGeometry x="100" y="60" width="200" height="40" as="geometry" />
</mxCell>
<mxCell id="4" value="(Object Reference)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#6B7280;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="100" y="95" width="200" height="30" as="geometry" />
</mxCell>
<mxCell id="5" value="对象实体" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#22C55E;" vertex="1" parent="1">
<mxGeometry x="560" y="60" width="200" height="40" as="geometry" />
</mxCell>
<mxCell id="6" value="(Object Instance)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#6B7280;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="560" y="95" width="200" height="30" as="geometry" />
</mxCell>
<mxCell id="7" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="80" y="160" width="260" height="140" as="geometry" />
</mxCell>
<mxCell id="8" value="stu = 0x1234" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#1E40AF;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="80" y="200" width="260" height="60" as="geometry" />
</mxCell>
<mxCell id="9" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=3;shadow=1;" vertex="1" parent="1">
<mxGeometry x="530" y="160" width="280" height="140" as="geometry" />
</mxCell>
<mxCell id="10" value="Student 对象" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="530" y="160" width="280" height="35" as="geometry" />
</mxCell>
<mxCell id="11" value="name = "张三"" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#166534;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="550" y="205" width="240" height="35" as="geometry" />
</mxCell>
<mxCell id="12" value="age = 20" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#166534;fontFamily=Consolas;" vertex="1" parent="1">
<mxGeometry x="550" y="245" width="240" height="35" as="geometry" />
</mxCell>
<mxCell id="13" value="" style="endArrow=blockThin;html=1;strokeWidth=5;strokeColor=#F59E0B;endFill=1;rounded=1;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="340" y="229.5" as="sourcePoint" />
<mxPoint x="530" y="229.5" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="14" value="栈内存" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="145" y="320" width="130" height="35" as="geometry" />
</mxCell>
<mxCell id="15" value="堆内存" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="605" y="320" width="130" height="35" as="geometry" />
</mxCell>
<mxCell id="16" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#93C5FD;strokeWidth=2;dashed=1;" vertex="1" parent="1">
<mxGeometry x="80" y="390" width="260" height="150" as="geometry" />
</mxCell>
<mxCell id="17" value="📦 存储" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="80" y="390" width="70" height="28" as="geometry" />
</mxCell>
<mxCell id="18" value="对象的内存地址" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=0;fontColor=#1E40AF;" vertex="1" parent="1">
<mxGeometry x="80" y="430" width="260" height="30" as="geometry" />
</mxCell>
<mxCell id="19" value="📐 大小" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="80" y="470" width="70" height="28" as="geometry" />
</mxCell>
<mxCell id="20" value="固定(32/64位)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=0;fontColor=#1E40AF;" vertex="1" parent="1">
<mxGeometry x="80" y="505" width="260" height="30" as="geometry" />
</mxCell>
<mxCell id="21" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#86EFAC;strokeWidth=2;dashed=1;" vertex="1" parent="1">
<mxGeometry x="530" y="390" width="280" height="150" as="geometry" />
</mxCell>
<mxCell id="22" value="📦 存储" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="530" y="390" width="70" height="28" as="geometry" />
</mxCell>
<mxCell id="23" value="对象的实际数据" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=0;fontColor=#166534;" vertex="1" parent="1">
<mxGeometry x="530" y="430" width="280" height="30" as="geometry" />
</mxCell>
<mxCell id="24" value="📐 大小" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
<mxGeometry x="530" y="470" width="70" height="28" as="geometry" />
</mxCell>
<mxCell id="25" value="取决于属性数量" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=15;fontStyle=0;fontColor=#166534;" vertex="1" parent="1">
<mxGeometry x="530" y="505" width="280" height="30" as="geometry" />
</mxCell>
<mxCell id="26" value="指向" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#B45309;rounded=1;" vertex="1" parent="1">
<mxGeometry x="405" y="200" width="60" height="28" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
3.2 气球与绳子类比
<mxfile>
<diagram id="balloon-rope-analogy" name="气球与绳子类比">
<mxGraphModel dx="1665" dy="796" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1000" pageHeight="900" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="860" width="920" x="40" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="气球与绳子类比" vertex="1">
<mxGeometry height="50" width="920" x="40" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EEF2FF;strokeColor=#A5B4FC;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="80" width="880" x="60" y="90" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#4338CA;" value="🎈 气球 = 对象实体(堆内存中的实际数据)" vertex="1">
<mxGeometry height="30" width="400" x="80" y="100" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#4338CA;" value="🧵 绳子 = 对象引用(栈内存中的地址变量)" vertex="1">
<mxGeometry height="30" width="400" x="80" y="135" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FCA5A5;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="200" width="880" x="60" y="190" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="规则 1:一条绳子可以不系气球" vertex="1">
<mxGeometry height="40" width="880" x="60" y="190" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#991B1B;rounded=1;fontFamily=Consolas;" value="Student stu = null;" vertex="1">
<mxGeometry height="40" width="280" x="100" y="290" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=40;fontStyle=0;" value="🧵" vertex="1">
<mxGeometry height="60" width="60" x="500" y="280" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#1E40AF;rounded=1;fontFamily=Consolas;" value="stu" vertex="1">
<mxGeometry height="30" width="60" x="555" y="295" as="geometry" />
</mxCell>
<mxCell id="12" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=4;strokeColor=#9CA3AF;endFill=1;dashed=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="620" y="310" as="sourcePoint" />
<mxPoint x="720" y="310" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=36;fontStyle=0;" value="❌" vertex="1">
<mxGeometry height="60" width="60" x="730" y="280" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=0;fontColor=#6B7280;" value="(没有气球)" vertex="1">
<mxGeometry height="30" width="100" x="790" y="295" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#FCD34D;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="220" width="880" x="60" y="410" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="规则 2:一条绳子只能系一个气球" vertex="1">
<mxGeometry height="40" width="880" x="60" y="410" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF9C3;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#92400E;rounded=1;fontFamily=Consolas;" value="Student stu = new Student("张三");" vertex="1">
<mxGeometry height="35" width="340" x="80" y="502.5" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF9C3;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#92400E;rounded=1;fontFamily=Consolas;" value="stu = new Student("李四"); // 原对象可能被 GC" vertex="1">
<mxGeometry height="35" width="400" x="90" y="542.5" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=40;fontStyle=0;" value="🧵" vertex="1">
<mxGeometry height="60" width="60" x="490" y="497.5" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#1E40AF;rounded=1;fontFamily=Consolas;" value="stu" vertex="1">
<mxGeometry height="30" width="60" x="545" y="512.5" as="geometry" />
</mxCell>
<mxCell id="21" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=4;strokeColor=#F59E0B;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="610" y="527.5" as="sourcePoint" />
<mxPoint x="700" y="527.5" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=40;fontStyle=0;" value="🎈" vertex="1">
<mxGeometry height="60" width="60" x="700" y="497.5" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#166534;rounded=1;" value="李四" vertex="1">
<mxGeometry height="30" width="60" x="760" y="512.5" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;fontStyle=0;opacity=50;" value="🎈" vertex="1">
<mxGeometry height="50" width="50" x="705" y="547.5" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=13;fontStyle=2;fontColor=#991B1B;rounded=1;" value="张三 (无人引用,等待 GC)" vertex="1">
<mxGeometry height="28" width="180" x="755" y="557.5" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="180" width="880" x="60" y="650" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="规则 3:多条绳子可以系同一个气球" vertex="1">
<mxGeometry height="40" width="880" x="60" y="650" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#166534;rounded=1;fontFamily=Consolas;" value="Student s1 = new Student("张三");" vertex="1">
<mxGeometry height="30" width="320" x="80" y="705" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#166534;rounded=1;fontFamily=Consolas;" value="Student s2 = s1;" vertex="1">
<mxGeometry height="30" width="180" x="84" y="745" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#166534;rounded=1;fontFamily=Consolas;" value="Student s3 = s1;" vertex="1">
<mxGeometry height="30" width="180" x="83" y="785" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;fontStyle=0;" value="🧵" vertex="1">
<mxGeometry height="50" width="50" x="500" y="690" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;rounded=1;fontFamily=Consolas;" value="s1" vertex="1">
<mxGeometry height="26" width="40" x="545" y="702" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;fontStyle=0;" value="🧵" vertex="1">
<mxGeometry height="50" width="50" x="500" y="735" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;rounded=1;fontFamily=Consolas;" value="s2" vertex="1">
<mxGeometry height="26" width="40" x="545" y="747" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=32;fontStyle=0;" value="🧵" vertex="1">
<mxGeometry height="50" width="50" x="500" y="780" as="geometry" />
</mxCell>
<mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#1E40AF;rounded=1;fontFamily=Consolas;" value="s3" vertex="1">
<mxGeometry height="26" width="40" x="545" y="792" as="geometry" />
</mxCell>
<mxCell id="37" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=4;strokeColor=#22C55E;endFill=1;edgeStyle=orthogonalEdgeStyle;rounded=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<Array as="points">
<mxPoint x="660" y="715" />
<mxPoint x="660" y="760" />
</Array>
<mxPoint x="590" y="715" as="sourcePoint" />
<mxPoint x="730" y="760" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="38" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=4;strokeColor=#22C55E;endFill=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<mxPoint x="590" y="760" as="sourcePoint" />
<mxPoint x="730" y="760" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="39" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=4;strokeColor=#22C55E;endFill=1;edgeStyle=orthogonalEdgeStyle;rounded=1;" value="">
<mxGeometry height="50" relative="1" width="50" as="geometry">
<Array as="points">
<mxPoint x="660" y="805" />
<mxPoint x="660" y="760" />
</Array>
<mxPoint x="590" y="805" as="sourcePoint" />
<mxPoint x="730" y="760" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=50;fontStyle=0;" value="🎈" vertex="1">
<mxGeometry height="70" width="70" x="730" y="725" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=#A855F7;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="张三" vertex="1">
<mxGeometry height="35" width="70" x="800" y="745" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
3.3 代码验证
public class ReferenceDemo {
public static void main(String[] args) {
// 规则 1:绳子可以不系气球
Student stu1 = null;
// stu1.getName(); // ❌ NullPointerException
// 规则 2:绳子只能系一个气球
Student stu2 = new Student("张三", 20);
System.out.println(stu2.getName()); // 张三
stu2 = new Student("李四", 21); // stu2 改系新气球
System.out.println(stu2.getName()); // 李四
// "张三"对象无人引用,等待 GC 回收
// 规则 3:多条绳子系同一个气球
Student s1 = new Student("王五", 22);
Student s2 = s1; // s2 系上 s1 的气球
Student s3 = s1; // s3 也系上同一个气球
// 验证:修改一处,全部生效
s2.setAge(30);
System.out.println(s1.getAge()); // 30
System.out.println(s3.getAge()); // 30
}
}
四、引用相等 vs 对象相等
4.1 概念对比
<mxfile>
<diagram id="java-equality-comparison" name="Java相等性比较">
<mxGraphModel dx="1040" dy="680" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="600" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<!-- 主容器背景 -->
<mxCell id="2" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#666666;strokeWidth=2;" vertex="1" parent="1">
<mxGeometry x="40" y="40" width="770" height="420" as="geometry" />
</mxCell>
<!-- 左侧区域:引用相等 -->
<mxCell id="3" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;strokeWidth=1;" vertex="1" parent="1">
<mxGeometry x="60" y="60" width="350" height="380" as="geometry" />
</mxCell>
<!-- 左侧标题 -->
<mxCell id="4" value="引用相等 (==)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;fontColor=#1a5276;" vertex="1" parent="1">
<mxGeometry x="130" y="80" width="200" height="40" as="geometry" />
</mxCell>
<!-- 左侧说明文字 -->
<mxCell id="5" value="比较:两个引用是否指向<br>同一个内存地址" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#333333;" vertex="1" parent="1">
<mxGeometry x="130" y="120" width="200" height="50" as="geometry" />
</mxCell>
<!-- 左侧 s1 变量 -->
<mxCell id="6" value="s1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontStyle=1;fontSize=14;" vertex="1" parent="1">
<mxGeometry x="120" y="190" width="70" height="40" as="geometry" />
</mxCell>
<!-- 左侧 s2 变量 -->
<mxCell id="7" value="s2" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontStyle=1;fontSize=14;" vertex="1" parent="1">
<mxGeometry x="270" y="190" width="70" height="40" as="geometry" />
</mxCell>
<!-- 左侧问号 -->
<mxCell id="8" value="?" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontStyle=1;fontColor=#e74c3c;" vertex="1" parent="1">
<mxGeometry x="200" y="250" width="60" height="30" as="geometry" />
</mxCell>
<!-- 左侧 == 符号 -->
<mxCell id="9" value="==" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontStyle=1;fontColor=#2980b9;" vertex="1" parent="1">
<mxGeometry x="200" y="275" width="60" height="30" as="geometry" />
</mxCell>
<!-- 左侧共享对象 -->
<mxCell id="10" value="同一对象<br><font style="font-size:11px;">0x1234</font>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;fontStyle=1;fontSize=14;" vertex="1" parent="1">
<mxGeometry x="130" y="340" width="200" height="60" as="geometry" />
</mxCell>
<!-- s1 到对象的箭头 -->
<mxCell id="11" value="" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#666666;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.25;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="6" target="10">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<!-- s2 到对象的箭头 -->
<mxCell id="12" value="" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#666666;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.75;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="7" target="10">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<!-- 右侧区域:对象相等 -->
<mxCell id="13" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;strokeWidth=1;" vertex="1" parent="1">
<mxGeometry x="440" y="60" width="350" height="380" as="geometry" />
</mxCell>
<!-- 右侧标题 -->
<mxCell id="14" value="对象相等 (equals)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;fontColor=#6c3483;" vertex="1" parent="1">
<mxGeometry x="510" y="80" width="200" height="40" as="geometry" />
</mxCell>
<!-- 右侧说明文字 -->
<mxCell id="15" value="比较:两个对象的<br>内容是否相同" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#333333;" vertex="1" parent="1">
<mxGeometry x="510" y="120" width="200" height="50" as="geometry" />
</mxCell>
<!-- 右侧 s1 变量 -->
<mxCell id="16" value="s1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontStyle=1;fontSize=14;" vertex="1" parent="1">
<mxGeometry x="500" y="190" width="70" height="40" as="geometry" />
</mxCell>
<!-- 右侧 s2 变量 -->
<mxCell id="17" value="s2" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontStyle=1;fontSize=14;" vertex="1" parent="1">
<mxGeometry x="650" y="190" width="70" height="40" as="geometry" />
</mxCell>
<!-- 右侧对象1 -->
<mxCell id="18" value="张三<br>20" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;fontStyle=1;fontSize=14;" vertex="1" parent="1">
<mxGeometry x="490" y="300" width="90" height="60" as="geometry" />
</mxCell>
<!-- 右侧对象2 -->
<mxCell id="19" value="张三<br>20" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;fontStyle=1;fontSize=14;" vertex="1" parent="1">
<mxGeometry x="640" y="300" width="90" height="60" as="geometry" />
</mxCell>
<!-- s1 到对象1的箭头 -->
<mxCell id="20" value="" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#666666;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="16" target="18">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<!-- s2 到对象2的箭头 -->
<mxCell id="21" value="" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#666666;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="17" target="19">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<!-- 右侧 == 符号 -->
<mxCell id="22" value="==" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;fontColor=#27ae60;" vertex="1" parent="1">
<mxGeometry x="580" y="315" width="60" height="30" as="geometry" />
</mxCell>
<!-- 右侧结果说明 -->
<mxCell id="23" value="内容相同则 equals = true" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontStyle=1;fontColor=#27ae60;" vertex="1" parent="1">
<mxGeometry x="500" y="380" width="220" height="40" as="geometry" />
</mxCell>
<!-- 分隔线 -->
<mxCell id="24" value="" style="endArrow=none;dashed=1;html=1;dashPattern=1 3;strokeWidth=2;rounded=0;strokeColor=#999999;" edge="1" parent="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="425" y="420" as="sourcePoint" />
<mxPoint x="425" y="80" as="targetPoint" />
</mxGeometry>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
4.2 代码示例
public class EqualityDemo {
public static void main(String[] args) {
Student s1 = new Student("张三", 20); // 对象 A
Student s2 = s1; // 也指向对象 A
Student s3 = new Student("张三", 20); // 对象 B(内容相同)
// ====== 引用相等(==):比较地址 ======
System.out.println(s1 == s2); // true (同一对象)
System.out.println(s1 == s3); // false (不同对象)
// ====== 对象相等(equals):比较内容 ======
// 注意:需要 Student 类重写 equals 方法才能正确比较
System.out.println(s1.equals(s3)); // 取决于 equals 实现
}
}
内存示意图:
<mxfile>
<diagram id="stack-heap-memory" name="栈内存与堆内存">
<mxGraphModel dx="1479" dy="692" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="550" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="280" width="220" x="40" y="60" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;fontColor=#1a5276;" value="栈内存" vertex="1">
<mxGeometry height="40" width="100" x="100" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontStyle=1;fontSize=13;align=left;spacingLeft=10;" value="s1 = 0x1234" vertex="1">
<mxGeometry height="40" width="140" x="60" y="90" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontStyle=1;fontSize=13;align=left;spacingLeft=10;" value="s2 = 0x1234" vertex="1">
<mxGeometry height="40" width="140" x="60" y="150" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontStyle=1;fontSize=13;align=left;spacingLeft=10;" value="s3 = 0x5678" vertex="1">
<mxGeometry height="40" width="140" x="60" y="250" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="340" width="240" x="340" y="60" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;fontColor=#2e7d32;" value="堆内存" vertex="1">
<mxGeometry height="40" width="100" x="410" y="20" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontStyle=0;fontSize=12;align=center;verticalAlign=top;spacingTop=5;" value="对象 A<br><hr style="margin:4px 0;">name = "张三"<br>age = 20<br><hr style="margin:4px 0;"><font color="#666666">地址: 0x1234</font>" vertex="1">
<mxGeometry height="120" width="160" x="380" y="80" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontStyle=0;fontSize=12;align=center;verticalAlign=top;spacingTop=5;" value="对象 B<br><hr style="margin:4px 0;">name = "张三"<br>age = 20<br><hr style="margin:4px 0;"><font color="#666666">地址: 0x5678</font>" vertex="1">
<mxGeometry height="120" width="160" x="380" y="240" as="geometry" />
</mxCell>
<mxCell id="11" edge="1" parent="1" source="4" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#d6b656;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;" target="9" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="12" edge="1" parent="1" source="5" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#d6b656;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.75;entryDx=0;entryDy=0;" target="9" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="13" edge="1" parent="1" source="6" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#9673a6;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.3;entryDx=0;entryDy=0;" target="10" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#666666;strokeWidth=1;dashed=1;" value="" vertex="1">
<mxGeometry height="120" width="580" x="20" y="410" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#333333;" value="引用比较结果 (==)" vertex="1">
<mxGeometry height="25" width="200" x="40" y="415" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;fontStyle=1;fontSize=12;" value="s1 == s2" vertex="1">
<mxGeometry height="30" width="80" x="40" y="450" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;" value="→" vertex="1">
<mxGeometry height="30" width="30" x="125" y="450" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#666666;" value="0x1234 == 0x1234" vertex="1">
<mxGeometry height="30" width="130" x="155" y="450" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;" value="→" vertex="1">
<mxGeometry height="30" width="30" x="285" y="450" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;fontStyle=1;fontSize=13;fontColor=#2e7d32;" value="true" vertex="1">
<mxGeometry height="30" width="60" x="320" y="450" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;fontStyle=1;fontSize=12;" value="s1 == s3" vertex="1">
<mxGeometry height="30" width="80" x="40" y="490" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;" value="→" vertex="1">
<mxGeometry height="30" width="30" x="125" y="490" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#666666;" value="0x1234 == 0x5678" vertex="1">
<mxGeometry height="30" width="130" x="155" y="490" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;" value="→" vertex="1">
<mxGeometry height="30" width="30" x="285" y="490" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;fontStyle=1;fontSize=13;fontColor=#b91c1c;" value="false" vertex="1">
<mxGeometry height="30" width="60" x="320" y="490" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=20;" value="💡" vertex="1">
<mxGeometry height="40" width="40" x="410" y="445" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#666666;" value="== 比较的是内存地址<br>而非对象内容" vertex="1">
<mxGeometry height="50" width="140" x="450" y="440" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
五、== 与 equals() 详解
5.1 == 运算符的行为
| 比较类型 | == 的作用 | 示例 |
|---|---|---|
| 基本类型 | 比较值 | 5 == 5 → true |
| 引用类型 | 比较地址 | s1 == s2 → 是否同一对象 |
// 基本类型用 == 比较值
int a = 100;
int b = 100;
System.out.println(a == b); // true(值相等)
// 引用类型用 == 比较地址
String str1 = new String("Java");
String str2 = new String("Java");
System.out.println(str1 == str2); // false(不同对象)
5.2 equals() 方法的行为
Object 类的默认实现(源码)
// java.lang.Object 类中的 equals 方法
public boolean equals(Object obj) {
return (this == obj); // 默认比较引用!
}
⚠️ 默认行为与 == 完全相同! 需要子类重写才能比较内容。
String 类的重写实现(源码解析)
// java.lang.String 类中的 equals 方法
public boolean equals(Object anObject) {
// 1️⃣ 先比较引用,相同直接返回 true
if (this == anObject) {
return true;
}
// 2️⃣ 检查类型是否是 String
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = value.length;
// 3️⃣ 比较长度
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
// 4️⃣ 逐字符比较
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
流程图:
<mxfile>
<diagram id="string-equals-flowchart-fixed" name="String.equals() 执行流程">
<mxGraphModel dx="1665" dy="796" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="800" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="840" width="860" x="30" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="String.equals() 方法执行流程图解" vertex="1">
<mxGeometry height="50" width="840" x="40" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rhombus;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#1E40AF;" value="this == anObject
(引用地址相同?)" vertex="1">
<mxGeometry height="90" width="180" x="350" y="100" as="geometry" />
</mxCell>
<mxCell id="5" edge="1" parent="1" source="4" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#16A34A;endFill=1;edgeStyle=orthogonalEdgeStyle;rounded=0;" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="350" y="145" as="sourcePoint" />
<mxPoint x="220" y="145" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#16A34A;rounded=1;" value="YES" vertex="1">
<mxGeometry height="20" width="40" x="270" y="120" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#FFFFFF;" value="return true" vertex="1">
<mxGeometry height="45" width="120" x="100" y="122.5" as="geometry" />
</mxCell>
<mxCell id="7" edge="1" parent="1" source="4" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#DC2626;endFill=1;edgeStyle=orthogonalEdgeStyle;rounded=0;" target="10" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="530" y="145" as="sourcePoint" />
<mxPoint x="600" y="145" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#DC2626;rounded=1;" value="NO" vertex="1">
<mxGeometry height="20" width="30" x="550" y="120" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="rhombus;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#1E40AF;" value="anObject
instanceof String?" vertex="1">
<mxGeometry height="80" width="160" x="600" y="105" as="geometry" />
</mxCell>
<mxCell id="13" edge="1" parent="1" source="10" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#DC2626;endFill=1;edgeStyle=orthogonalEdgeStyle;rounded=0;" value="">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="820" y="145" />
</Array>
<mxPoint x="760" y="145" as="sourcePoint" />
<mxPoint x="820" y="230" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#DC2626;rounded=1;" value="NO" vertex="1">
<mxGeometry height="20" width="30" x="780" y="120" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#FFFFFF;" value="return false" vertex="1">
<mxGeometry height="45" width="120" x="760" y="230" as="geometry" />
</mxCell>
<mxCell id="11" edge="1" parent="1" source="10" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#16A34A;endFill=1;edgeStyle=orthogonalEdgeStyle;rounded=0;" target="17" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="680" y="185" as="sourcePoint" />
<mxPoint x="680" y="230" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#16A34A;rounded=1;" value="YES" vertex="1">
<mxGeometry height="20" width="40" x="690" y="195" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="rhombus;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#1E40AF;" value="长度 & 编码
相同?" vertex="1">
<mxGeometry height="80" width="140" x="610" y="230" as="geometry" />
</mxCell>
<mxCell id="20" edge="1" parent="1" source="17" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#DC2626;endFill=1;edgeStyle=orthogonalEdgeStyle;rounded=0;" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="680" y="310" as="sourcePoint" />
<mxPoint x="680" y="370" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#DC2626;rounded=1;" value="NO" vertex="1">
<mxGeometry height="20" width="30" x="690" y="330" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#FFFFFF;" value="return false" vertex="1">
<mxGeometry height="45" width="120" x="620" y="370" as="geometry" />
</mxCell>
<mxCell id="18" edge="1" parent="1" source="17" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#16A34A;endFill=1;edgeStyle=orthogonalEdgeStyle;rounded=0;" target="23" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="610" y="270" as="sourcePoint" />
<mxPoint x="440" y="270" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#16A34A;rounded=1;" value="YES" vertex="1">
<mxGeometry height="20" width="40" x="530" y="245" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="rhombus;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#92400E;" value="逐字符比较
全部相同?" vertex="1">
<mxGeometry height="90" width="160" x="280" y="225" as="geometry" />
</mxCell>
<mxCell id="24" edge="1" parent="1" source="23" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#16A34A;endFill=1;edgeStyle=orthogonalEdgeStyle;rounded=0;" target="28" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="280" y="275" as="sourcePoint" />
<mxPoint x="160" y="275" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#16A34A;rounded=1;" value="YES" vertex="1">
<mxGeometry height="20" width="40" x="210" y="245" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#FFFFFF;" value="return true" vertex="1">
<mxGeometry height="45" width="120" x="60" y="247.5" as="geometry" />
</mxCell>
<mxCell id="26" edge="1" parent="1" source="23" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#DC2626;endFill=1;edgeStyle=orthogonalEdgeStyle;rounded=0;" target="29" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="360" y="320" as="sourcePoint" />
<mxPoint x="360" y="370" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#DC2626;rounded=1;" value="NO" vertex="1">
<mxGeometry height="20" width="30" x="370" y="330" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#FFFFFF;" value="return false" vertex="1">
<mxGeometry height="45" width="120" x="300" y="370" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="390" width="780" x="70" y="450" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💻 JDK 源码解析 (简化版)" vertex="1">
<mxGeometry height="35" width="220" x="70" y="450" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;whiteSpace=wrap;rounded=0;" value="<pre style="color:#cbd5e1; font-family:'Consolas', monospace; font-size:13px;">
<span style="color:#c678dd;">public</span> <span style="color:#e5c07b;">boolean</span> <span style="color:#61afef;">equals</span>(<span style="color:#e5c07b;">Object</span> anObject) {
 <span style="color:#7f848e;">// 1. 引用比较:如果是同一个对象,直接返回 true</span>
 <span style="color:#c678dd;">if</span> (<span style="color:#e06c75;">this</span> == anObject) {
 <span style="color:#c678dd;">return</span> <span style="color:#d19a66;">true</span>;
 }
 
 <span style="color:#7f848e;">// 2. 类型检查:必须是 String 类型</span>
 <span style="color:#c678dd;">if</span> (anObject <span style="color:#c678dd;">instanceof</span> <span style="color:#e5c07b;">String</span>) {
 <span style="color:#e5c07b;">String</span> aString = (<span style="color:#e5c07b;">String</span>)anObject;
 
 <span style="color:#7f848e;">// 3. 编码/长度检查 & 4. 逐字符比较</span>
 <span style="color:#c678dd;">if</span> (coder() == aString.coder()) {
 <span style="color:#c678dd;">return</span> isLatin1() 
 ? StringLatin1.equals(value, aString.value)
 : StringUTF16.equals(value, aString.value);
 }
 }
 <span style="color:#7f848e;">// 类型不匹配或内容不同,返回 false</span>
 <span style="color:#c678dd;">return</span> <span style="color:#d19a66;">false</span>;
}
</pre>" vertex="1">
<mxGeometry height="340" width="740" x="90" y="490" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
5.3 自定义类重写 equals()
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj) {
// 1. 引用相同
if (this == obj) return true;
// 2. null 检查 + 类型检查
if (obj == null || getClass() != obj.getClass()) return false;
// 3. 强制类型转换
Student student = (Student) obj;
// 4. 比较各属性
return age == student.age &&
Objects.equals(name, student.name);
}
// 重写 equals 必须重写 hashCode(后续笔记详解)
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
5.4 == vs equals() 对比总结表
| 比较方式 | 基本类型 | 引用类型(未重写) | 引用类型(已重写) |
|---|---|---|---|
== |
比较值 | 比较地址 | 比较地址 |
equals() |
❌ 不适用 | 比较地址(同==) | 比较内容 |
六、字符串的特殊情况
6.1 字符串常量池
<mxfile>
<diagram id="string-constant-pool" name="字符串常量池机制">
<mxGraphModel dx="1388" dy="663" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1200" pageHeight="900" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="890" width="1140" x="40" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="字符串常量池机制" vertex="1">
<mxGeometry height="50" width="1140" x="30" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="130" width="1080" x="60" y="90" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💻 代码示例" vertex="1">
<mxGeometry height="30" width="110" x="60" y="90" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#22C55E;fontFamily=Consolas;fontStyle=1;" value="String str1 = "Java";" vertex="1">
<mxGeometry height="25" width="250" x="90" y="130" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#94A3B8;fontFamily=Consolas;" value="// 字面量方式,存入常量池" vertex="1">
<mxGeometry height="25" width="250" x="350" y="130" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#3B82F6;fontFamily=Consolas;fontStyle=1;" value="String str2 = "Java";" vertex="1">
<mxGeometry height="25" width="250" x="90" y="158" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#94A3B8;fontFamily=Consolas;" value="// 复用常量池中已有的对象" vertex="1">
<mxGeometry height="25" width="250" x="350" y="158" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=15;fontColor=#F59E0B;fontFamily=Consolas;fontStyle=1;" value="String str3 = new String("Java");" vertex="1">
<mxGeometry height="25" width="350" x="90" y="186" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#94A3B8;fontFamily=Consolas;" value="// 强制在堆中创建新对象" vertex="1">
<mxGeometry height="25" width="250" x="380" y="186" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="结果验证" vertex="1">
<mxGeometry height="28" width="90" x="750" y="92" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#22C55E;fontFamily=Consolas;fontStyle=1;" value="str1 == str2 → true" vertex="1">
<mxGeometry height="25" width="200" x="840" y="128" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#EF4444;fontFamily=Consolas;fontStyle=1;" value="str1 == str3 → false" vertex="1">
<mxGeometry height="25" width="200" x="840" y="158" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#22C55E;fontFamily=Consolas;fontStyle=1;" value="str1.equals(str3) → true" vertex="1">
<mxGeometry height="25" width="250" x="840" y="188" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ECFDF5;strokeColor=#10B981;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="380" width="220" x="60" y="240" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📚 栈内存 (Stack)" vertex="1">
<mxGeometry height="40" width="220" x="60" y="240" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#D1FAE5;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#065F46;rounded=1;" value="局部变量表" vertex="1">
<mxGeometry height="30" width="180" x="80" y="295" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#FFFFFF;" value="str1" vertex="1">
<mxGeometry height="40" width="80" x="100" y="345" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#FFFFFF;" value="str2" vertex="1">
<mxGeometry height="40" width="80" x="100" y="410" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#FFFFFF;" value="str3" vertex="1">
<mxGeometry height="40" width="80" x="100" y="475" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#065F46;" value="存储引用地址" vertex="1">
<mxGeometry height="25" width="180" x="80" y="540" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#6B7280;" value="指向堆中对象 →" vertex="1">
<mxGeometry height="25" width="180" x="80" y="565" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;" value="" vertex="1">
<mxGeometry height="380" width="820" x="340" y="240" as="geometry" />
</mxCell>
<mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🗄️ 堆内存 (Heap)" vertex="1">
<mxGeometry height="40" width="820" x="340" y="240" as="geometry" />
</mxCell>
<mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;dashed=1;" value="" vertex="1">
<mxGeometry height="200" width="350" x="370" y="300" as="geometry" />
</mxCell>
<mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🏊 字符串常量池 (String Pool)" vertex="1">
<mxGeometry height="35" width="350" x="370" y="300" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#22C55E;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="120" width="160" x="400" y="360" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value=""Java"" vertex="1">
<mxGeometry height="35" width="120" x="420" y="375" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#166534;rounded=1;" value="地址: 0x1111" vertex="1">
<mxGeometry height="25" width="120" x="420" y="420" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#166534;fontStyle=2;" value="str1 & str2 共享" vertex="1">
<mxGeometry height="20" width="120" x="420" y="450" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#E5E7EB;strokeColor=#9CA3AF;strokeWidth=1;fontSize=12;fontColor=#6B7280;" value=""Hello"" vertex="1">
<mxGeometry height="35" width="100" x="590" y="370" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#E5E7EB;strokeColor=#9CA3AF;strokeWidth=1;fontSize=12;fontColor=#6B7280;" value=""World"" vertex="1">
<mxGeometry height="35" width="100" x="590" y="415" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#9CA3AF;" value="..." vertex="1">
<mxGeometry height="25" width="40" x="620" y="455" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="180" width="340" x="780" y="320" as="geometry" />
</mxCell>
<mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📦 String 对象 (new 创建)" vertex="1">
<mxGeometry height="35" width="340" x="780" y="320" as="geometry" />
</mxCell>
<mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#92400E;rounded=1;" value="地址: 0x2222" vertex="1">
<mxGeometry height="25" width="120" x="810" y="365" as="geometry" />
</mxCell>
<mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#374151;fontStyle=1;" value="value:" vertex="1">
<mxGeometry height="25" width="60" x="810" y="400" as="geometry" />
</mxCell>
<mxCell id="39" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=1;fontSize=12;fontColor=#991B1B;fontFamily=Consolas;" value="char[] {'J','a','v','a'}" vertex="1">
<mxGeometry height="30" width="150" x="870" y="398" as="geometry" />
</mxCell>
<mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontColor=#374151;fontStyle=1;" value="hash:" vertex="1">
<mxGeometry height="25" width="60" x="810" y="435" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=#E5E7EB;align=center;verticalAlign=middle;fontSize=12;fontColor=#6B7280;rounded=1;" value="0 (未计算)" vertex="1">
<mxGeometry height="25" width="100" x="870" y="435" as="geometry" />
</mxCell>
<mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#92400E;fontStyle=2;" value="str3 独占此对象" vertex="1">
<mxGeometry height="20" width="120" x="860" y="470" as="geometry" />
</mxCell>
<mxCell id="43" edge="1" parent="1" source="19" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#22C55E;endFill=1;edgeStyle=orthogonalEdgeStyle;rounded=1;entryX=0;entryY=0.25;entryDx=0;entryDy=0;exitX=1;exitY=0.5;exitDx=0;exitDy=0;" target="28" value="">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="300" y="365" />
<mxPoint x="300" y="390" />
</Array>
<mxPoint x="200" y="365" as="sourcePoint" />
<mxPoint x="400" y="420" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="44" edge="1" parent="1" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#3B82F6;endFill=1;edgeStyle=orthogonalEdgeStyle;rounded=1;entryX=0.008;entryY=0.58;entryDx=0;entryDy=0;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryPerimeter=0;" target="28" value="">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="380" y="430" />
</Array>
<mxPoint x="160" y="430" as="sourcePoint" />
<mxPoint x="380" y="420" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="45" edge="1" parent="1" source="21" style="endArrow=blockThin;html=1;strokeWidth=3;strokeColor=#F59E0B;endFill=1;edgeStyle=orthogonalEdgeStyle;rounded=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" target="35" value="">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="280" y="495" />
<mxPoint x="280" y="560" />
<mxPoint x="760" y="560" />
<mxPoint x="760" y="410" />
</Array>
<mxPoint x="200" y="495" as="sourcePoint" />
<mxPoint x="780" y="410" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="46" edge="1" parent="1" source="39" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#EF4444;endFill=1;dashed=1;edgeStyle=orthogonalEdgeStyle;rounded=1;" target="28" value="">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="945" y="520" />
<mxPoint x="480" y="520" />
</Array>
<mxPoint x="870" y="413" as="sourcePoint" />
<mxPoint x="560" y="420" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=10;fontColor=#991B1B;rounded=1;" value="内部引用常量池" vertex="1">
<mxGeometry height="20" width="100" x="580" y="510" as="geometry" />
</mxCell>
<mxCell id="48" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#94A3B8;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="220" width="1100" x="60" y="640" as="geometry" />
</mxCell>
<mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💡 核心要点总结" vertex="1">
<mxGeometry height="35" width="160" x="60" y="640" as="geometry" />
</mxCell>
<mxCell id="50" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="150" width="330" x="80" y="690" as="geometry" />
</mxCell>
<mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="✅ 字面量创建" vertex="1">
<mxGeometry height="30" width="120" x="80" y="690" as="geometry" />
</mxCell>
<mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#166534;fontFamily=Consolas;fontStyle=1;" value="String s = "Java";" vertex="1">
<mxGeometry height="25" width="200" x="100" y="730" as="geometry" />
</mxCell>
<mxCell id="53" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;fontSize=12;fontColor=#166534;" value="• 先检查常量池是否存在
• 存在则直接返回引用
• 不存在则创建并加入池
• 多个变量共享同一对象" vertex="1">
<mxGeometry height="75" width="290" x="100" y="760" as="geometry" />
</mxCell>
<mxCell id="54" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="150" width="330" x="430" y="690" as="geometry" />
</mxCell>
<mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="⚠️ new 创建" vertex="1">
<mxGeometry height="30" width="100" x="430" y="690" as="geometry" />
</mxCell>
<mxCell id="56" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#92400E;fontFamily=Consolas;fontStyle=1;" value="String s = new String("Java");" vertex="1">
<mxGeometry height="25" width="290" x="450" y="730" as="geometry" />
</mxCell>
<mxCell id="57" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=top;fontSize=12;fontColor=#92400E;" value="• 强制在堆中创建新对象
• 即使常量池已有相同值
• 内部 value 仍指向常量池
• 浪费内存,应避免使用" vertex="1">
<mxGeometry height="75" width="290" x="450" y="760" as="geometry" />
</mxCell>
<mxCell id="58" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="150" width="360" x="780" y="690" as="geometry" />
</mxCell>
<mxCell id="59" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔍 比较方式" vertex="1">
<mxGeometry height="30" width="100" x="780" y="690" as="geometry" />
</mxCell>
<mxCell id="60" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="== 比较" vertex="1">
<mxGeometry height="22" width="70" x="800" y="735" as="geometry" />
</mxCell>
<mxCell id="61" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;" value="比较引用地址(内存地址)" vertex="1">
<mxGeometry height="22" width="200" x="880" y="735" as="geometry" />
</mxCell>
<mxCell id="62" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value=".equals()" vertex="1">
<mxGeometry height="22" width="70" x="800" y="770" as="geometry" />
</mxCell>
<mxCell id="63" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;" value="比较字符串内容(推荐)" vertex="1">
<mxGeometry height="22" width="200" x="880" y="770" as="geometry" />
</mxCell>
<mxCell id="64" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontColor=#1E40AF;rounded=1;fontStyle=1;" value="💡 实际开发中,始终使用 equals() 比较字符串内容" vertex="1">
<mxGeometry height="25" width="320" x="800" y="805" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
6.2 代码验证
public class StringPoolDemo {
public static void main(String[] args) {
// 字面量创建(使用常量池)
String str1 = "Java";
String str2 = "Java";
// new 创建(强制新对象)
String str3 = new String("Java");
String str4 = new String("Java");
// ====== == 比较(地址)======
System.out.println("str1 == str2: " + (str1 == str2)); // true ✅ 常量池复用
System.out.println("str1 == str3: " + (str1 == str3)); // false ❌ 不同对象
System.out.println("str3 == str4: " + (str3 == str4)); // false ❌ 不同对象
// ====== equals 比较(内容)======
System.out.println("str1.equals(str3): " + str1.equals(str3)); // true ✅
System.out.println("str3.equals(str4): " + str3.equals(str4)); // true ✅
// ====== intern() 方法 ======
String str5 = str3.intern(); // 返回常量池中的引用
System.out.println("str1 == str5: " + (str1 == str5)); // true ✅
}
}
输出结果:
str1 == str2: true
str1 == str3: false
str3 == str4: false
str1.equals(str3): true
str3.equals(str4): true
str1 == str5: true
6.3 字符串创建方式对比
| 创建方式 | 代码示例 | 使用常量池 | 内存位置 |
|---|---|---|---|
| 字面量 | "Java" |
✅ 是 | 常量池 |
| new | new String("Java") |
❌ 否 | 堆内存 |
| intern() | str.intern() |
✅ 是 | 返回常量池引用 |
| 拼接(常量) | "Ja" + "va" |
✅ 是 | 编译期优化 |
| 拼接(变量) | str + "va" |
❌ 否 | 运行时新建 |
七、Integer 缓存池
7.1 缓存池机制
public class IntegerCacheDemo {
public static void main(String[] args) {
// -128 ~ 127 范围内使用缓存
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 ✅
}
}
7.2 Integer.valueOf() 源码
// java.lang.Integer 类
public static Integer valueOf(int i) {
// 如果在缓存范围内,返回缓存对象
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
// 否则创建新对象
return new Integer(i);
}
// 缓存池内部类
private static class IntegerCache {
static final int low = -128;
static final int high = 127; // 可通过 JVM 参数调整
static final Integer cache[];
static {
// 初始化缓存数组
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
}
流程图:
<mxfile>
<diagram id="integer-valueof-flow" name="Integer.valueOf执行流程">
<mxGraphModel dx="1021" dy="478" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="700" pageHeight="500" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#666666;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="420" width="620" x="40" y="40" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#4a4a4a;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;fontColor=#ffffff;" value="Integer.valueOf(i) 执行流程" vertex="1">
<mxGeometry height="45" width="620" x="40" y="40" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rhombus;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;strokeWidth=2;fontSize=14;fontStyle=1;" value="i 在 [-128, 127] ?" vertex="1">
<mxGeometry height="80" width="200" x="250" y="110" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#27ae60;" value="YES" vertex="1">
<mxGeometry height="30" width="50" x="130" y="195" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#e74c3c;" value="NO" vertex="1">
<mxGeometry height="30" width="50" x="520" y="195" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;strokeWidth=2;fontSize=14;fontStyle=1;" value="返回缓存池中的对象<br><font style="font-size:12px;" color="#666666">cache[i + 128]</font>" vertex="1">
<mxGeometry height="70" width="180" x="80" y="250" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;strokeWidth=2;fontSize=14;fontStyle=1;" value="new Integer(i)<br><font style="font-size:12px;" color="#666666">创建新对象</font>" vertex="1">
<mxGeometry height="70" width="180" x="440" y="250" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#2e7d32;" value="相同值 == 返回 true ✓" vertex="1">
<mxGeometry height="50" width="180" x="80" y="380" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#b91c1c;" value="相同值 == 返回 false ✗" vertex="1">
<mxGeometry height="50" width="180" x="440" y="380" as="geometry" />
</mxCell>
<mxCell id="11" edge="1" parent="1" source="4" style="endArrow=classic;html=1;rounded=1;strokeWidth=2;strokeColor=#82b366;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="7" value="">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="170" y="150" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="12" edge="1" parent="1" source="4" style="endArrow=classic;html=1;rounded=1;strokeWidth=2;strokeColor=#b85450;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="8" value="">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="530" y="150" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="13" edge="1" parent="1" source="7" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#82b366;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="9" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="14" edge="1" parent="1" source="8" style="endArrow=classic;html=1;rounded=0;strokeWidth=2;strokeColor=#b85450;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" target="10" value="">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" value="♻️" vertex="1">
<mxGeometry height="40" width="50" x="130" y="330" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;" value="🆕" vertex="1">
<mxGeometry height="40" width="50" x="520" y="330" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
7.3 各包装类缓存范围
| 包装类 | 缓存范围 | 说明 |
|---|---|---|
| Byte | -128 ~ 127 | 全部缓存 |
| Short | -128 ~ 127 | |
| Integer | -128 ~ 127 | high 可通过 JVM 参数调整 |
| Long | -128 ~ 127 | |
| Character | 0 ~ 127 | |
| Boolean | TRUE / FALSE | 只有两个值 |
| Float | ❌ 无缓存 | 浮点数范围太大 |
| Double | ❌ 无缓存 | 浮点数范围太大 |
八、最佳实践总结
8.1 比较规则速查表
| 场景 | 推荐方式 | 示例 |
|---|---|---|
| 基本类型比较 | <mark> |
int a </mark> int b |
| 对象内容比较 | equals() |
str1.equals(str2) |
| 对象是否同一个 | <mark> |
obj1 </mark> obj2 |
| null 安全比较 | Objects.equals() |
Objects.equals(a, b) |
8.2 常见陷阱
// ❌ 陷阱 1:用 == 比较字符串内容
String input = new String("admin");
if (input == "admin") { // false!
// 永远不会执行
}
// ✅ 正确写法
if (input.equals("admin")) {
// 正确执行
}
// ✅ 更安全的写法(避免 NullPointerException)
if ("admin".equals(input)) {
// 常量在前,避免 input 为 null 时报错
}
// ❌ 陷阱 2:用 == 比较包装类
Integer a = 200;
Integer b = 200;
if (a == b) { // false!超出缓存范围
// 可能不会执行
}
// ✅ 正确写法
if (a.equals(b)) {
// 正确执行
}
8.3 核心要点记忆图
<mxfile>
<diagram id="core-summary-complete" name="核心要点总结">
<mxGraphModel dx="1959" dy="936" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1200" pageHeight="900" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#64748B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="980" width="1140" x="45" y="20" as="geometry" />
</mxCell>
<mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🎯 Java 核心机制:对象、引用与内存模型总结" vertex="1">
<mxGeometry height="55" width="1140" x="45" y="20" as="geometry" />
</mxCell>
<mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ECFDF5;strokeColor=#10B981;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="190" width="530" x="60" y="95" as="geometry" />
</mxCell>
<mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="1️⃣ new 创建对象机制" vertex="1">
<mxGeometry height="40" width="220" x="60" y="95" as="geometry" />
</mxCell>
<mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="55" width="490" x="80" y="150" as="geometry" />
</mxCell>
<mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontColor=#22C55E;fontFamily=Consolas;fontStyle=1;" value="ClassName ref = new ClassName(args);" vertex="1">
<mxGeometry height="35" width="470" x="90" y="160" as="geometry" />
</mxCell>
<mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="📚 引用 (Reference)" vertex="1">
<mxGeometry height="35" width="120" x="100" y="220" as="geometry" />
</mxCell>
<mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontColor=#1E40AF;rounded=1;" value="存储在栈内存 (Stack)" vertex="1">
<mxGeometry height="22" width="120" x="100" y="255" as="geometry" />
</mxCell>
<mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#6B7280;fontStyle=1;" value="→ 指向 →" vertex="1">
<mxGeometry height="25" width="70" x="220" y="225" as="geometry" />
</mxCell>
<mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="🎈 对象 (Object)" vertex="1">
<mxGeometry height="35" width="110" x="290" y="220" as="geometry" />
</mxCell>
<mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=11;fontColor=#92400E;rounded=1;" value="存储在堆内存 (Heap)" vertex="1">
<mxGeometry height="22" width="110" x="290" y="255" as="geometry" />
</mxCell>
<mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#D1FAE5;align=center;verticalAlign=middle;fontSize=12;fontColor=#065F46;rounded=1;fontStyle=1;" value="💡 关键字 new 总是会在堆中开辟新空间" vertex="1">
<mxGeometry height="25" width="230" x="330" y="120" as="geometry" />
</mxCell>
<mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="190" width="560" x="610" y="95" as="geometry" />
</mxCell>
<mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="2️⃣ 引用 vs 对象" vertex="1">
<mxGeometry height="40" width="170" x="610" y="95" as="geometry" />
</mxCell>
<mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#94A3B8;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="120" width="520" x="630" y="150" as="geometry" />
</mxCell>
<mxCell id="17" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#8B5CF6;strokeColor=#7C3AED;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="🪢 引用 = 绳子" vertex="1">
<mxGeometry height="40" width="120" x="650" y="165" as="geometry" />
</mxCell>
<mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#EDE9FE;align=center;verticalAlign=middle;fontSize=11;fontColor=#6D28D9;rounded=1;" value="持有地址 (Address)" vertex="1">
<mxGeometry height="20" width="120" x="650" y="205" as="geometry" />
</mxCell>
<mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EC4899;strokeColor=#DB2777;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="🎈 对象 = 气球" vertex="1">
<mxGeometry height="40" width="120" x="790" y="165" as="geometry" />
</mxCell>
<mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=#FCE7F3;align=center;verticalAlign=middle;fontSize=11;fontColor=#BE185D;rounded=1;" value="持有数据 (Data)" vertex="1">
<mxGeometry height="20" width="120" x="790" y="205" as="geometry" />
</mxCell>
<mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=13;fontColor=#92400E;rounded=1;fontStyle=1;" value="多个引用可同时指向同一个对象 (共享)" vertex="1">
<mxGeometry height="25" width="260" x="650" y="235" as="geometry" />
</mxCell>
<mxCell id="22" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="ref1" vertex="1">
<mxGeometry height="30" width="45" x="940" y="165" as="geometry" />
</mxCell>
<mxCell id="23" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=2;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="ref2" vertex="1">
<mxGeometry height="30" width="45" x="942.5" y="205" as="geometry" />
</mxCell>
<mxCell id="24" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=2;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="Object" vertex="1">
<mxGeometry height="40" width="60" x="1080" y="175" as="geometry" />
</mxCell>
<mxCell id="25" edge="1" parent="1" source="22" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#22C55E;endFill=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.3;entryDx=0;entryDy=0;" target="24" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="1000" y="175" as="sourcePoint" />
<mxPoint x="1080" y="187" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="26" edge="1" parent="1" source="23" style="endArrow=blockThin;html=1;strokeWidth=2;strokeColor=#3B82F6;endFill=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.7;entryDx=0;entryDy=0;" target="24" value="">
<mxGeometry relative="1" as="geometry">
<mxPoint x="1000" y="215" as="sourcePoint" />
<mxPoint x="1080" y="203" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="270" width="530" x="60" y="305" as="geometry" />
</mxCell>
<mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="3️⃣ == vs equals()" vertex="1">
<mxGeometry height="40" width="180" x="60" y="305" as="geometry" />
</mxCell>
<mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="100" width="230" x="80" y="360" as="geometry" />
</mxCell>
<mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="== 运算符" vertex="1">
<mxGeometry height="28" width="100" x="80" y="360" as="geometry" />
</mxCell>
<mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#FECACA;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#991B1B;rounded=1;" value="基本类型" vertex="1">
<mxGeometry height="22" width="70" x="95" y="398" as="geometry" />
</mxCell>
<mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#991B1B;" value="比较值" vertex="1">
<mxGeometry height="22" width="60" x="175" y="398" as="geometry" />
</mxCell>
<mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=#FECACA;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#991B1B;rounded=1;" value="引用类型" vertex="1">
<mxGeometry height="22" width="70" x="95" y="428" as="geometry" />
</mxCell>
<mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#991B1B;" value="比较地址" vertex="1">
<mxGeometry height="22" width="70" x="175" y="428" as="geometry" />
</mxCell>
<mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="100" width="240" x="330" y="360" as="geometry" />
</mxCell>
<mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value=".equals() 方法" vertex="1">
<mxGeometry height="28" width="130" x="330" y="360" as="geometry" />
</mxCell>
<mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#BBF7D0;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#166534;rounded=1;" value="默认行为" vertex="1">
<mxGeometry height="22" width="65" x="345" y="398" as="geometry" />
</mxCell>
<mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#166534;" value="比较地址(同==)" vertex="1">
<mxGeometry height="22" width="120" x="420" y="398" as="geometry" />
</mxCell>
<mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=#BBF7D0;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#166534;rounded=1;" value="重写后" vertex="1">
<mxGeometry height="22" width="65" x="345" y="428" as="geometry" />
</mxCell>
<mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#166534;fontStyle=1;" value="比较内容 ✓" vertex="1">
<mxGeometry height="22" width="100" x="420" y="428" as="geometry" />
</mxCell>
<mxCell id="41" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#475569;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="85" width="490" x="80" y="475" as="geometry" />
</mxCell>
<mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💻 代码示例" vertex="1">
<mxGeometry height="24" width="90" x="80" y="475" as="geometry" />
</mxCell>
<mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#E2E8F0;fontFamily=Consolas;" value="String a = "Java", b = "Java", c = new String("Java");" vertex="1">
<mxGeometry height="20" width="450" x="95" y="505" as="geometry" />
</mxCell>
<mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#F59E0B;fontFamily=Consolas;fontStyle=1;" value="a == b → true a == c → false a.equals(c) → true" vertex="1">
<mxGeometry height="20" width="450" x="95" y="530" as="geometry" />
</mxCell>
<mxCell id="45" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EDE9FE;strokeColor=#8B5CF6;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="270" width="560" x="610" y="305" as="geometry" />
</mxCell>
<mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="4️⃣ 常量池 / 缓存池 (Pools)" vertex="1">
<mxGeometry height="40" width="240" x="610" y="305" as="geometry" />
</mxCell>
<mxCell id="47" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="95" width="250" x="630" y="360" as="geometry" />
</mxCell>
<mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📝 String 字符串常量池" vertex="1">
<mxGeometry height="26" width="180" x="630" y="360" as="geometry" />
</mxCell>
<mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=#BFDBFE;align=center;verticalAlign=middle;fontSize=13;fontColor=#1E40AF;fontFamily=Consolas;fontStyle=1;rounded=1;" value=""Java"" vertex="1">
<mxGeometry height="25" width="70" x="650" y="395" as="geometry" />
</mxCell>
<mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#1E40AF;" value="字面量方式" vertex="1">
<mxGeometry height="25" width="80" x="730" y="395" as="geometry" />
</mxCell>
<mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=11;fontColor=#166534;fontStyle=1;rounded=1;" value="→ 常量池复用 ✓" vertex="1">
<mxGeometry height="22" width="110" x="650" y="425" as="geometry" />
</mxCell>
<mxCell id="52" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="95" width="250" x="900" y="360" as="geometry" />
</mxCell>
<mxCell id="53" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🔢 Integer 缓存池" vertex="1">
<mxGeometry height="26" width="150" x="900" y="360" as="geometry" />
</mxCell>
<mxCell id="54" parent="1" style="text;html=1;strokeColor=none;fillColor=#FDE68A;align=center;verticalAlign=middle;fontSize=13;fontColor=#92400E;fontFamily=Consolas;fontStyle=1;rounded=1;" value="-128 ~ 127" vertex="1">
<mxGeometry height="25" width="90" x="920" y="395" as="geometry" />
</mxCell>
<mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#92400E;" value="范围内" vertex="1">
<mxGeometry height="25" width="50" x="1020" y="395" as="geometry" />
</mxCell>
<mxCell id="56" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=11;fontColor=#166534;fontStyle=1;rounded=1;" value="→ 缓存池复用 ✓" vertex="1">
<mxGeometry height="22" width="110" x="920" y="425" as="geometry" />
</mxCell>
<mxCell id="57" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="90" width="520" x="630" y="470" as="geometry" />
</mxCell>
<mxCell id="58" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="⚠️ 超出范围或强制 new" vertex="1">
<mxGeometry height="26" width="200" x="630" y="470" as="geometry" />
</mxCell>
<mxCell id="59" parent="1" style="text;html=1;strokeColor=none;fillColor=#FECACA;align=center;verticalAlign=middle;fontSize=12;fontColor=#991B1B;fontFamily=Consolas;rounded=1;" value="new String("Java")" vertex="1">
<mxGeometry height="22" width="150" x="650" y="505" as="geometry" />
</mxCell>
<mxCell id="60" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#991B1B;fontStyle=1;" value="→ 创建新对象 ✗" vertex="1">
<mxGeometry height="22" width="110" x="810" y="505" as="geometry" />
</mxCell>
<mxCell id="61" parent="1" style="text;html=1;strokeColor=none;fillColor=#FECACA;align=center;verticalAlign=middle;fontSize=12;fontColor=#991B1B;fontFamily=Consolas;rounded=1;" value="Integer i = 200" vertex="1">
<mxGeometry height="22" width="150" x="650" y="532" as="geometry" />
</mxCell>
<mxCell id="62" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#991B1B;fontStyle=1;" value="→ 创建新对象 ✗" vertex="1">
<mxGeometry height="22" width="110" x="810" y="532" as="geometry" />
</mxCell>
<mxCell id="63" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=11;fontColor=#991B1B;rounded=1;fontStyle=2;" value="💡 避免不必要的 new,利用池化机制节省内存" vertex="1">
<mxGeometry height="40" width="200" x="930" y="505" as="geometry" />
</mxCell>
<mxCell id="64" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#64748B;strokeWidth=3;shadow=1;" value="" vertex="1">
<mxGeometry height="345" width="1110" x="60" y="595" as="geometry" />
</mxCell>
<mxCell id="65" parent="1" style="text;html=1;strokeColor=none;fillColor=#64748B;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📊 内存模型可视化 (Stack vs Heap)" vertex="1">
<mxGeometry height="40" width="300" x="60" y="595" as="geometry" />
</mxCell>
<mxCell id="66" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#E0F2FE;strokeColor=#0284C7;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="245" width="200" x="90" y="655" as="geometry" />
</mxCell>
<mxCell id="67" parent="1" style="text;html=1;strokeColor=none;fillColor=#0284C7;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📚 栈内存 (Stack)" vertex="1">
<mxGeometry height="30" width="200" x="90" y="655" as="geometry" />
</mxCell>
<mxCell id="68" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#0284C7;strokeWidth=1;fontSize=12;" value="String a" vertex="1">
<mxGeometry height="40" width="160" x="110" y="700" as="geometry" />
</mxCell>
<mxCell id="68_val" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0F9FF;align=center;verticalAlign=middle;fontSize=11;fontFamily=Consolas;fontColor=#0369A1;" value="0x100" vertex="1">
<mxGeometry height="20" width="40" x="220" y="710" as="geometry" />
</mxCell>
<mxCell id="69" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#0284C7;strokeWidth=1;fontSize=12;" value="String b" vertex="1">
<mxGeometry height="40" width="160" x="110" y="760" as="geometry" />
</mxCell>
<mxCell id="69_val" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0F9FF;align=center;verticalAlign=middle;fontSize=11;fontFamily=Consolas;fontColor=#0369A1;" value="0x100" vertex="1">
<mxGeometry height="20" width="40" x="220" y="770" as="geometry" />
</mxCell>
<mxCell id="70" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#0284C7;strokeWidth=1;fontSize=12;" value="String c" vertex="1">
<mxGeometry height="40" width="160" x="110" y="820" as="geometry" />
</mxCell>
<mxCell id="70_val" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0F9FF;align=center;verticalAlign=middle;fontSize=11;fontFamily=Consolas;fontColor=#0369A1;" value="0x200" vertex="1">
<mxGeometry height="20" width="40" x="220" y="830" as="geometry" />
</mxCell>
<mxCell id="71" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#16A34A;strokeWidth=2;" value="" vertex="1">
<mxGeometry height="280" width="600" x="400" y="640" as="geometry" />
</mxCell>
<mxCell id="72" parent="1" style="text;html=1;strokeColor=none;fillColor=#16A34A;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="🎈 堆内存 (Heap)" vertex="1">
<mxGeometry height="30" width="600" x="400" y="655" as="geometry" />
</mxCell>
<mxCell id="73" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF9C3;strokeColor=#CA8A04;strokeWidth=2;dashed=1;" value="" vertex="1">
<mxGeometry height="150" width="250" x="440" y="700" as="geometry" />
</mxCell>
<mxCell id="74" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#854D0E;" value="📝 字符串常量池 (String Pool)" vertex="1">
<mxGeometry height="25" width="180" x="440" y="700" as="geometry" />
</mxCell>
<mxCell id="75" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#FBBF24;strokeColor=#B45309;strokeWidth=2;fontFamily=Consolas;fontSize=14;fontColor=#FFFFFF;fontStyle=1;" value=""Java"" vertex="1">
<mxGeometry height="60" width="100" x="515" y="740" as="geometry" />
</mxCell>
<mxCell id="75_addr" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#B45309;fontFamily=Consolas;" value="Addr: 0x100" vertex="1">
<mxGeometry height="20" width="80" x="525" y="805" as="geometry" />
</mxCell>
<mxCell id="76" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#F472B6;strokeColor=#BE185D;strokeWidth=2;fontFamily=Consolas;fontSize=13;fontColor=#FFFFFF;fontStyle=1;" value="new String()" vertex="1">
<mxGeometry height="70" width="120" x="810" y="805" as="geometry" />
</mxCell>
<mxCell id="76_inner" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#FFFFFF;" value="(Wrap "Java")" vertex="1">
<mxGeometry height="20" width="80" x="830" y="845" as="geometry" />
</mxCell>
<mxCell id="76_addr" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#BE185D;fontFamily=Consolas;" value="Addr: 0x200" vertex="1">
<mxGeometry height="20" width="80" x="830" y="880" as="geometry" />
</mxCell>
<mxCell id="link1" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.032;entryY=0.367;entryDx=0;entryDy=0;fillColor=#a20025;strokeColor=#0284C7;strokeWidth=2;entryPerimeter=0;" target="75">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="330" y="720" />
<mxPoint x="330" y="762" />
</Array>
<mxPoint x="270" y="720" as="sourcePoint" />
<mxPoint x="485" y="770" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="link1_lbl" connectable="0" parent="link1" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];fontSize=10;fontColor=#0284C7;" value="引用" vertex="1">
<mxGeometry relative="1" x="-0.2" as="geometry">
<mxPoint x="16" y="-12" as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="link2" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=1;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.046;entryY=0.655;entryDx=0;entryDy=0;fillColor=#a20025;strokeColor=#0284C7;strokeWidth=2;entryPerimeter=0;" target="75">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="500" y="780" />
</Array>
<mxPoint x="270" y="779" as="sourcePoint" />
<mxPoint x="499.98" y="770" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="link2_lbl" connectable="0" parent="link2" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];fontSize=10;fontColor=#0284C7;" value="复用引用" vertex="1">
<mxGeometry relative="1" x="-0.3" as="geometry">
<mxPoint x="2" y="11" as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="link3" edge="1" parent="1" source="70" style="edgeStyle=orthogonalEdgeStyle;rounded=1;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;fillColor=#a20025;strokeColor=#BE185D;strokeWidth=2;" target="76">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="800" y="840" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="link3_lbl" connectable="0" parent="link3" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];fontSize=10;fontColor=#BE185D;" value="指向新对象" vertex="1">
<mxGeometry relative="1" x="-0.1" as="geometry">
<mxPoint as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="link4" edge="1" parent="1" source="76" style="edgeStyle=orthogonalEdgeStyle;rounded=1;orthogonalLoop=1;jettySize=auto;html=1;entryX=1;entryY=0.5;entryDx=0;entryDy=0;fillColor=#a20025;strokeColor=#CA8A04;strokeWidth=1;dashed=1;" target="75">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="860" y="770" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="link4_lbl" connectable="0" parent="link4" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];fontSize=10;fontColor=#CA8A04;" value="内部引用" vertex="1">
<mxGeometry relative="1" as="geometry">
<mxPoint as="offset" />
</mxGeometry>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
💬 评论