数据类型详解


一、数据类型概览

1.1 类型体系

<mxfile>
  <diagram id="java-data-types" name="Java数据类型体系">
    <mxGraphModel dx="1068" dy="484" 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="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#1E293B;" value="Java 数据类型体系" vertex="1">
          <mxGeometry height="40" width="300" x="300" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#6366F1;strokeColor=#4F46E5;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="数据类型" vertex="1">
          <mxGeometry height="44" width="140" x="380" y="70" as="geometry" />
        </mxCell>
        <mxCell id="4" edge="1" parent="1" source="3" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#64748B;strokeWidth=2;endArrow=none;endFill=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="450" y="140" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="5" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#64748B;strokeWidth=2;endArrow=classic;endFill=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="230" y="140" />
            </Array>
            <mxPoint x="450" y="140" as="sourcePoint" />
            <mxPoint x="230" y="170" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="6" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#64748B;strokeWidth=2;endArrow=classic;endFill=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="670" y="140" />
            </Array>
            <mxPoint x="450" y="140" as="sourcePoint" />
            <mxPoint x="670" y="170" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="基本类型&lt;br&gt;&lt;font style=&quot;font-size: 10px;&quot;&gt;(Primitive)&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="150" x="155" y="170" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="引用类型&lt;br&gt;&lt;font style=&quot;font-size: 10px;&quot;&gt;(Reference)&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="150" x="595" y="170" as="geometry" />
        </mxCell>
        <mxCell id="9" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#3B82F6;strokeWidth=2;endArrow=none;endFill=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="230" y="220" as="sourcePoint" />
            <mxPoint x="230" y="250" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="10" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#3B82F6;strokeWidth=2;endArrow=classic;endFill=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="90" y="250" />
            </Array>
            <mxPoint x="230" y="250" as="sourcePoint" />
            <mxPoint x="90" y="280" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="11" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#3B82F6;strokeWidth=2;endArrow=classic;endFill=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="230" y="250" as="sourcePoint" />
            <mxPoint x="230" y="280" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="12" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#3B82F6;strokeWidth=2;endArrow=classic;endFill=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="370" y="250" />
            </Array>
            <mxPoint x="230" y="250" as="sourcePoint" />
            <mxPoint x="370" y="280" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#1E40AF;" value="整数" vertex="1">
          <mxGeometry height="30" width="100" x="40" y="280" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#1E40AF;" value="浮点" vertex="1">
          <mxGeometry height="30" width="100" x="180" y="280" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#1E40AF;" value="其他" vertex="1">
          <mxGeometry height="30" width="100" x="320" y="280" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#94A3B8;strokeWidth=1;fontSize=11;fontFamily=Courier New;fontColor=#475569;align=center;" value="byte&lt;br&gt;short&lt;br&gt;int&lt;br&gt;long" vertex="1">
          <mxGeometry height="80" width="100" x="40" y="320" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#94A3B8;strokeWidth=1;fontSize=11;fontFamily=Courier New;fontColor=#475569;align=center;" value="float&lt;br&gt;double" vertex="1">
          <mxGeometry height="80" width="100" x="180" y="320" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#94A3B8;strokeWidth=1;fontSize=11;fontFamily=Courier New;fontColor=#475569;align=center;" value="char&lt;br&gt;boolean" vertex="1">
          <mxGeometry height="80" width="100" x="320" y="320" as="geometry" />
        </mxCell>
        <mxCell id="19" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#22C55E;strokeWidth=2;endArrow=none;endFill=0;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="670" y="220" as="sourcePoint" />
            <mxPoint x="670" y="250" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="20" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#22C55E;strokeWidth=2;endArrow=classic;endFill=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="530" y="250" />
            </Array>
            <mxPoint x="670" y="250" as="sourcePoint" />
            <mxPoint x="530" y="280" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="21" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#22C55E;strokeWidth=2;endArrow=classic;endFill=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="670" y="250" as="sourcePoint" />
            <mxPoint x="670" y="280" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="22" edge="1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#22C55E;strokeWidth=2;endArrow=classic;endFill=1;" value="">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="810" y="250" />
            </Array>
            <mxPoint x="670" y="250" as="sourcePoint" />
            <mxPoint x="810" y="280" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#166534;" value="类&lt;br&gt;&lt;font style=&quot;font-size: 10px;&quot;&gt;Class&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="480" y="280" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#166534;" value="接口&lt;br&gt;&lt;font style=&quot;font-size: 10px;&quot;&gt;Interface&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="620" y="280" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#166534;" value="数组&lt;br&gt;&lt;font style=&quot;font-size: 10px;&quot;&gt;Array&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="760" y="280" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#94A3B8;strokeWidth=1;fontSize=11;fontFamily=Courier New;fontColor=#475569;align=center;" value="String&lt;br&gt;Object&lt;br&gt;..." vertex="1">
          <mxGeometry height="80" width="100" x="480" y="320" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#94A3B8;strokeWidth=1;fontSize=11;fontFamily=Courier New;fontColor=#475569;align=center;" value="List&lt;br&gt;Map&lt;br&gt;..." vertex="1">
          <mxGeometry height="80" width="100" x="620" y="320" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#94A3B8;strokeWidth=1;fontSize=11;fontFamily=Courier New;fontColor=#475569;align=center;" value="int[]&lt;br&gt;String[]&lt;br&gt;..." vertex="1">
          <mxGeometry height="80" width="100" x="760" y="320" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#3B82F6;strokeWidth=2;dashed=1;" value="" vertex="1">
          <mxGeometry height="70" width="380" x="40" y="430" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=8;" value="&lt;b&gt;基本类型&lt;/b&gt;:8 种,直接存储值,效率高" vertex="1">
          <mxGeometry height="40" width="360" x="50" y="445" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;dashed=1;" value="" vertex="1">
          <mxGeometry height="70" width="420" x="460" y="430" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#166534;spacingLeft=8;" value="&lt;b&gt;引用类型&lt;/b&gt;:存储对象的内存地址(引用),指向堆中的对象" vertex="1">
          <mxGeometry height="40" width="400" x="470" y="445" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=1;fontSize=10;fontStyle=1;fontColor=#FFFFFF;" value="8种" vertex="1">
          <mxGeometry height="20" width="30" x="305" y="185" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=10;fontStyle=1;fontColor=#FFFFFF;" value="∞" vertex="1">
          <mxGeometry height="25" width="30" x="745" y="185" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

1.2 基本类型一览表

<mxfile>
  <diagram id="java-primitive-types" name="8种基本数据类型">
    <mxGraphModel dx="1068" dy="484" 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="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=22;fontStyle=1;fontColor=#1E293B;" value="8 种基本数据类型" vertex="1">
          <mxGeometry height="40" width="300" x="300" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#6366F1;strokeColor=#4F46E5;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="类型" vertex="1">
          <mxGeometry height="40" width="100" x="40" y="70" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#6366F1;strokeColor=#4F46E5;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="字节" vertex="1">
          <mxGeometry height="40" width="60" x="140" y="70" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#6366F1;strokeColor=#4F46E5;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="位" vertex="1">
          <mxGeometry height="40" width="50" x="200" y="70" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#6366F1;strokeColor=#4F46E5;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="默认值" vertex="1">
          <mxGeometry height="40" width="90" x="250" y="70" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#6366F1;strokeColor=#4F46E5;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="取值范围" vertex="1">
          <mxGeometry height="40" width="520" x="340" y="70" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#93C5FD;strokeWidth=1;fontSize=12;fontStyle=1;fontFamily=Courier New;fontColor=#1E40AF;" value="byte" vertex="1">
          <mxGeometry height="60" width="100" x="40" y="120" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;" value="1" vertex="1">
          <mxGeometry height="60" width="60" x="140" y="120" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;" value="8" vertex="1">
          <mxGeometry height="60" width="50" x="200" y="120" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontFamily=Courier New;fontColor=#334155;" value="0" vertex="1">
          <mxGeometry height="60" width="90" x="250" y="120" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;align=center;" value="-128 ~ 127&lt;br&gt;&lt;font style=&quot;font-size: 10px;&quot; color=&quot;#64748b&quot;&gt;(-2⁷ ~ 2⁷-1)&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="520" x="340" y="120" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#93C5FD;strokeWidth=1;fontSize=12;fontStyle=1;fontFamily=Courier New;fontColor=#1E40AF;" value="short" vertex="1">
          <mxGeometry height="60" width="100" x="40" y="180" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;" value="2" vertex="1">
          <mxGeometry height="60" width="60" x="140" y="180" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;" value="16" vertex="1">
          <mxGeometry height="60" width="50" x="200" y="180" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontFamily=Courier New;fontColor=#334155;" value="0" vertex="1">
          <mxGeometry height="60" width="90" x="250" y="180" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;align=center;" value="-32768 ~ 32767&lt;br&gt;&lt;font style=&quot;font-size: 10px;&quot; color=&quot;#64748b&quot;&gt;(-2¹⁵ ~ 2¹⁵-1)&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="520" x="340" y="180" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#93C5FD;strokeWidth=1;fontSize=12;fontStyle=1;fontFamily=Courier New;fontColor=#1E40AF;" value="int" vertex="1">
          <mxGeometry height="60" width="100" x="40" y="240" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;" value="4" vertex="1">
          <mxGeometry height="60" width="60" x="140" y="240" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;" value="32" vertex="1">
          <mxGeometry height="60" width="50" x="200" y="240" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontFamily=Courier New;fontColor=#334155;" value="0" vertex="1">
          <mxGeometry height="60" width="90" x="250" y="240" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;align=center;" value="-2147483648 ~ 2147483647&lt;br&gt;&lt;font style=&quot;font-size: 10px;&quot; color=&quot;#64748b&quot;&gt;(-2³¹ ~ 2³¹-1)&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="520" x="340" y="240" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#93C5FD;strokeWidth=1;fontSize=12;fontStyle=1;fontFamily=Courier New;fontColor=#1E40AF;" value="long" vertex="1">
          <mxGeometry height="60" width="100" x="40" y="300" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;" value="8" vertex="1">
          <mxGeometry height="60" width="60" x="140" y="300" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;" value="64" vertex="1">
          <mxGeometry height="60" width="50" x="200" y="300" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontFamily=Courier New;fontColor=#334155;" value="0L" vertex="1">
          <mxGeometry height="60" width="90" x="250" y="300" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;align=center;" value="-2⁶³ ~ 2⁶³-1&lt;br&gt;&lt;font style=&quot;font-size: 10px;&quot; color=&quot;#64748b&quot;&gt;(约 ±922亿亿)&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="520" x="340" y="300" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#FCD34D;strokeWidth=1;fontSize=12;fontStyle=1;fontFamily=Courier New;fontColor=#92400E;" value="float" vertex="1">
          <mxGeometry height="60" width="100" x="40" y="360" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontColor=#334155;" value="4" vertex="1">
          <mxGeometry height="60" width="60" x="140" y="360" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontColor=#334155;" value="32" vertex="1">
          <mxGeometry height="60" width="50" x="200" y="360" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontFamily=Courier New;fontColor=#334155;" value="0.0f" vertex="1">
          <mxGeometry height="60" width="90" x="250" y="360" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontColor=#334155;align=center;" value="±1.4E-45 ~ ±3.4E38&lt;br&gt;&lt;font style=&quot;font-size: 10px;&quot; color=&quot;#64748b&quot;&gt;(约 7 位有效数字)&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="520" x="340" y="360" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#FCD34D;strokeWidth=1;fontSize=12;fontStyle=1;fontFamily=Courier New;fontColor=#92400E;" value="double" vertex="1">
          <mxGeometry height="60" width="100" x="40" y="420" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF9C3;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontColor=#334155;" value="8" vertex="1">
          <mxGeometry height="60" width="60" x="140" y="420" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF9C3;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontColor=#334155;" value="64" vertex="1">
          <mxGeometry height="60" width="50" x="200" y="420" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF9C3;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontFamily=Courier New;fontColor=#334155;" value="0.0d" vertex="1">
          <mxGeometry height="60" width="90" x="250" y="420" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF9C3;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontColor=#334155;align=center;" value="±4.9E-324 ~ ±1.8E308&lt;br&gt;&lt;font style=&quot;font-size: 10px;&quot; color=&quot;#64748b&quot;&gt;(约 15 位有效数字)&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="520" x="340" y="420" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontSize=12;fontStyle=1;fontFamily=Courier New;fontColor=#166534;" value="char" vertex="1">
          <mxGeometry height="60" width="100" x="40" y="480" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontColor=#334155;" value="2" vertex="1">
          <mxGeometry height="60" width="60" x="140" y="480" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontColor=#334155;" value="16" vertex="1">
          <mxGeometry height="60" width="50" x="200" y="480" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#BBF7D0;strokeWidth=1;fontSize=11;fontFamily=Courier New;fontColor=#334155;" value="&#39;\u0000&#39;" vertex="1">
          <mxGeometry height="60" width="90" x="250" y="480" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontColor=#334155;align=center;" value="0 ~ 65535&lt;br&gt;&lt;font style=&quot;font-size: 10px;&quot; color=&quot;#64748b&quot;&gt;(Unicode 字符)&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="520" x="340" y="480" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FCE7F3;strokeColor=#F9A8D4;strokeWidth=1;fontSize=12;fontStyle=1;fontFamily=Courier New;fontColor=#9D174D;" value="boolean" vertex="1">
          <mxGeometry height="60" width="100" x="40" y="540" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FDF2F8;strokeColor=#FBCFE8;strokeWidth=1;fontSize=12;fontColor=#334155;" value="1*" vertex="1">
          <mxGeometry height="60" width="60" x="140" y="540" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FDF2F8;strokeColor=#FBCFE8;strokeWidth=1;fontSize=12;fontColor=#334155;" value="-" vertex="1">
          <mxGeometry height="60" width="50" x="200" y="540" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FDF2F8;strokeColor=#FBCFE8;strokeWidth=1;fontSize=12;fontFamily=Courier New;fontColor=#334155;" value="false" vertex="1">
          <mxGeometry height="60" width="90" x="250" y="540" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FDF2F8;strokeColor=#FBCFE8;strokeWidth=1;fontSize=12;fontColor=#334155;align=center;" value="true / false&lt;br&gt;&lt;font style=&quot;font-size: 10px;&quot; color=&quot;#64748b&quot;&gt;(*JVM 规范未明确定义)&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="520" x="340" y="540" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="整数类型" vertex="1">
          <mxGeometry height="16" width="50" x="40" y="120" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="浮点类型" vertex="1">
          <mxGeometry height="16" width="50" x="40" y="360" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="字符类型" vertex="1">
          <mxGeometry height="16" width="50" x="40" y="480" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=#EC4899;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="布尔类型" vertex="1">
          <mxGeometry height="16" width="50" x="40" y="540" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=none;strokeColor=#6366F1;strokeWidth=3;dashed=0;" value="" vertex="1">
          <mxGeometry height="240" width="820" x="40" y="120" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=none;strokeColor=#F59E0B;strokeWidth=3;dashed=0;" value="" vertex="1">
          <mxGeometry height="120" width="820" x="40" y="360" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=none;strokeColor=#64748B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="555" width="880" x="10" y="55" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=#F1F5F9;align=center;verticalAlign=middle;fontSize=12;fontColor=#475569;rounded=1;spacingLeft=8;spacingRight=8;" value="💡 记忆技巧:1-2-4-8 字节递增,整数默认 int,浮点默认 double" vertex="1">
          <mxGeometry height="30" width="540" x="180" y="620" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

二、整数类型详解

2.1 取值范围的本质

<mxfile>
  <diagram id="byte-range-explanation" name="为什么byte的范围是-128~127">
    <mxGraphModel dx="1111" dy="980" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=20;fontStyle=1;fontColor=#1a1a2e;" value="为什么 byte 的范围是 -128 ~ 127?" vertex="1">
          <mxGeometry height="40" width="600" x="150" y="20" 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=14;fontColor=#666666;" value="计算机使用补码表示有符号整数" vertex="1">
          <mxGeometry height="30" width="400" x="250" y="60" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1f5fe;strokeColor=#0288d1;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="80" width="260" x="160" y="100" 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=13;" value="&lt;b&gt;byte 占 8 位&lt;/b&gt;&lt;br&gt;最高位是符号位&lt;br&gt;&lt;font color=&quot;#0288d1&quot;&gt;0 = 正数 &amp;nbsp;│&amp;nbsp; 1 = 负数&lt;/font&gt;" vertex="1">
          <mxGeometry height="80" width="260" x="160" y="100" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#c62828;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="60" width="40" x="450" y="110" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=10;fontColor=#c62828;" value="符号位" vertex="1">
          <mxGeometry height="20" width="60" x="440" y="170" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#388e3c;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="60" width="245" x="495" y="110" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=10;fontColor=#388e3c;" value="数值位 (7位)" vertex="1">
          <mxGeometry height="20" width="100" x="570" y="170" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=9;fontColor=#999999;" value="bit7" vertex="1">
          <mxGeometry height="20" width="40" x="450" y="90" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=9;fontColor=#999999;" value="bit6 ~ bit0" vertex="1">
          <mxGeometry height="20" width="100" x="570" y="90" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="340" width="380" x="50" y="210" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#e65100;" value="二进制补码 → 十进制值" vertex="1">
          <mxGeometry height="30" width="380" x="50" y="220" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#81c784;" value="" vertex="1">
          <mxGeometry height="120" width="340" x="70" y="260" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;" value="&lt;font face=&quot;Courier New&quot;&gt;0111 1111&lt;/font&gt;  →  &lt;b&gt;+127&lt;/b&gt;  (最大正数)" vertex="1">
          <mxGeometry height="25" width="300" x="90" y="265" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;" value="&lt;font face=&quot;Courier New&quot;&gt;0111 1110&lt;/font&gt;  →  &lt;b&gt;+126&lt;/b&gt;" vertex="1">
          <mxGeometry height="25" width="300" x="90" y="290" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#666666;" value="..." vertex="1">
          <mxGeometry height="20" width="300" x="90" y="310" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;" value="&lt;font face=&quot;Courier New&quot;&gt;0000 0001&lt;/font&gt;  →  &lt;b&gt;+1&lt;/b&gt;" vertex="1">
          <mxGeometry height="25" width="300" x="90" y="330" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;" value="&lt;font face=&quot;Courier New&quot;&gt;0000 0000&lt;/font&gt;  →  &lt;b&gt; 0&lt;/b&gt;" vertex="1">
          <mxGeometry height="25" width="300" x="90" y="355" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffebee;strokeColor=#e57373;" value="" vertex="1">
          <mxGeometry height="145" width="340" x="70" y="390" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;" value="&lt;font face=&quot;Courier New&quot;&gt;1111 1111&lt;/font&gt;  →  &lt;b&gt;-1&lt;/b&gt;" vertex="1">
          <mxGeometry height="25" width="300" x="90" y="395" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;" value="&lt;font face=&quot;Courier New&quot;&gt;1111 1110&lt;/font&gt;  →  &lt;b&gt;-2&lt;/b&gt;" vertex="1">
          <mxGeometry height="25" width="300" x="90" y="420" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#666666;" value="..." vertex="1">
          <mxGeometry height="20" width="300" x="90" y="445" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;" value="&lt;font face=&quot;Courier New&quot;&gt;1000 0001&lt;/font&gt;  →  &lt;b&gt;-127&lt;/b&gt;" vertex="1">
          <mxGeometry height="25" width="300" x="90" y="465" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;" value="&lt;font face=&quot;Courier New&quot;&gt;1000 0000&lt;/font&gt;  →  &lt;b&gt;-128&lt;/b&gt;  (最小负数)" vertex="1">
          <mxGeometry height="25" width="300" x="90" y="495" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#5c6bc0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="340" width="380" x="460" y="210" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#3949ab;" value="关键点" vertex="1">
          <mxGeometry height="30" width="380" x="460" y="220" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#66bb6a;" value="" vertex="1">
          <mxGeometry height="60" width="340" x="480" y="260" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;b&gt;正数范围&lt;/b&gt;&lt;br&gt;0 ~ 2⁷-1 = &lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;0 ~ 127&lt;/b&gt;&lt;/font&gt; (共 128 个)" vertex="1">
          <mxGeometry height="60" width="340" x="480" y="260" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#ef5350;" value="" vertex="1">
          <mxGeometry height="60" width="340" x="480" y="335" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;b&gt;负数范围&lt;/b&gt;&lt;br&gt;-2⁷ ~ -1 = &lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;-128 ~ -1&lt;/b&gt;&lt;/font&gt; (共 128 个)" vertex="1">
          <mxGeometry height="60" width="340" x="480" y="335" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#42a5f5;" value="" vertex="1">
          <mxGeometry height="60" width="340" x="480" y="410" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;b&gt;总共&lt;/b&gt;&lt;br&gt;&lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;256&lt;/b&gt;&lt;/font&gt; 个不同的值 (2⁸ = 256)" vertex="1">
          <mxGeometry height="60" width="340" x="480" y="410" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff9c4;strokeColor=#fbc02d;" value="" vertex="1">
          <mxGeometry height="50" width="340" x="480" y="485" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="💡 最大值要减1是因为 &lt;b&gt;0 占用了一个正数位置&lt;/b&gt;" vertex="1">
          <mxGeometry height="50" width="340" x="480" y="485" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=#ab47bc;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="790" x="50" y="570" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;b&gt;结论:&lt;/b&gt;byte 范围 = 128个负数 (-128 ~ -1) + 1个零 (0) + 127个正数 (1 ~ 127) = &lt;font color=&quot;#7b1fa2&quot;&gt;&lt;b&gt;-128 ~ 127&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="790" x="50" y="570" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

2.2 整数字面量

// ═══════════════════════════════════════════════════════════════════════
// 整数字面量的各种表示形式
// ═══════════════════════════════════════════════════════════════════════

// 十进制(默认)
int decimal = 100;

// 二进制(0b 或 0B 开头,JDK 7+)
int binary = 0b1100100;      // = 100
int binary2 = 0B1010_1010;   // 可用下划线分隔,提高可读性

// 八进制(0 开头)
int octal = 0144;            // = 100

// 十六进制(0x 或 0X 开头)
int hex = 0x64;              // = 100
int hex2 = 0xFF;             // = 255

// ═══════════════════════════════════════════════════════════════════════
// 长整型必须加 L 后缀
// ═══════════════════════════════════════════════════════════════════════

long small = 100;            // OK,自动转换
long big = 10000000000L;     // 必须加 L,否则编译错误
// long error = 10000000000; // ❌ 编译错误:整数太大

// 建议使用大写 L,小写 l 容易与数字 1 混淆
long value = 123456789L;     // ✅ 推荐
long value2 = 123456789l;    // ⚠️ 不推荐

// ═══════════════════════════════════════════════════════════════════════
// 数字中的下划线(JDK 7+)
// ═══════════════════════════════════════════════════════════════════════

int million = 1_000_000;           // 一百万,更易读
long creditCard = 1234_5678_9012_3456L;
int binary3 = 0b1010_0101_1100_0011;

// 下划线的限制
// int error1 = _100;      // ❌ 不能在开头
// int error2 = 100_;      // ❌ 不能在结尾
// int error3 = 0_x64;     // ❌ 不能在进制标识中
// double error4 = 3_.14;  // ❌ 不能在小数点旁边
<mxfile>
  <diagram id="java-integer-literals" name="整数字面量表示形式">
    <mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="820" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#1E293B;" value="Java 整数字面量表示形式" vertex="1">
          <mxGeometry height="40" width="400" x="250" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="进制表示" vertex="1">
          <mxGeometry height="30" width="100" x="40" y="70" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EEF2FF;strokeColor=#6366F1;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="200" width="820" x="40" y="110" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="十进制" vertex="1">
          <mxGeometry height="35" width="180" x="60" y="130" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;int decimal = 100;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="60" y="170" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#64748B;spacingLeft=5;" value="默认格式,直接书写数字" vertex="1">
          <mxGeometry height="20" width="180" x="60" y="200" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="二进制 &lt;font color=&quot;#ef4444&quot;&gt;(JDK 7+)&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="180" x="260" y="130" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;int bin = &lt;b&gt;0b&lt;/b&gt;1100100;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="260" y="170" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#64748B;spacingLeft=5;" value="&lt;b&gt;0b&lt;/b&gt; 或 &lt;b&gt;0B&lt;/b&gt; 开头" vertex="1">
          <mxGeometry height="20" width="180" x="260" y="200" 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="八进制" vertex="1">
          <mxGeometry height="35" width="180" x="460" y="130" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;int octal = &lt;b&gt;0&lt;/b&gt;144;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="460" y="170" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#64748B;spacingLeft=5;" value="&lt;b&gt;0&lt;/b&gt; 开头(数字零)" vertex="1">
          <mxGeometry height="20" width="180" x="460" y="200" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EC4899;strokeColor=#DB2777;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="十六进制" vertex="1">
          <mxGeometry height="35" width="180" x="660" y="130" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=12;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;int hex = &lt;b&gt;0x&lt;/b&gt;64;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="180" x="660" y="170" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#64748B;spacingLeft=5;" value="&lt;b&gt;0x&lt;/b&gt; 或 &lt;b&gt;0X&lt;/b&gt; 开头" vertex="1">
          <mxGeometry height="20" width="180" x="660" y="200" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#3B82F6;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#1E40AF;" value="= 100" vertex="1">
          <mxGeometry height="30" width="60" x="120" y="230" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#166534;" value="= 100" vertex="1">
          <mxGeometry height="30" width="60" x="320" y="230" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#92400E;" value="= 100" vertex="1">
          <mxGeometry height="30" width="60" x="520" y="230" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#FCE7F3;strokeColor=#EC4899;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#9D174D;" value="= 100" vertex="1">
          <mxGeometry height="30" width="60" x="720" y="230" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=#FDF2F8;align=center;verticalAlign=middle;fontSize=10;fontColor=#9D174D;rounded=1;" value="&lt;font face=&quot;Courier New&quot;&gt;0xFF = 255&lt;/font&gt;" vertex="1">
          <mxGeometry height="20" width="70" x="715" y="270" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=center;verticalAlign=middle;fontSize=10;fontColor=#166534;rounded=1;" value="&lt;font face=&quot;Courier New&quot;&gt;0b1010_1010&lt;/font&gt;" vertex="1">
          <mxGeometry height="20" width="90" x="260" y="270" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#64748B;" value="可用下划线分隔" vertex="1">
          <mxGeometry height="20" width="85" x="355" y="270" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="长整型 L 后缀" vertex="1">
          <mxGeometry height="30" width="120" x="40" y="320" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F5F3FF;strokeColor=#8B5CF6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="150" width="820" x="40" y="360" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#FFFFFF;" value="✅ 正确用法" vertex="1">
          <mxGeometry height="25" width="360" x="60" y="375" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;long small = 100;&lt;/font&gt;            &lt;font color=&quot;#64748b&quot;&gt;// 自动转换&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="360" x="60" y="405" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;long big = 10000000000&lt;b&gt;L&lt;/b&gt;;&lt;/font&gt;   &lt;font color=&quot;#64748b&quot;&gt;// 必须加 L&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="360" x="60" y="433" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#86EFAC;strokeWidth=1;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;long value = 123456789&lt;b&gt;L&lt;/b&gt;;&lt;/font&gt;  &lt;font color=&quot;#22c55e&quot;&gt;// 推荐大写 L&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="360" x="60" y="461" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#FFFFFF;" value="❌ 错误用法" vertex="1">
          <mxGeometry height="25" width="400" x="440" y="375" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=1;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;long error = 10000000000;&lt;/font&gt;  &lt;font color=&quot;#ef4444&quot;&gt;// 编译错误:整数太大&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="400" x="440" y="405" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;long value2 = 123456789&lt;b&gt;l&lt;/b&gt;;&lt;/font&gt; &lt;font color=&quot;#f59e0b&quot;&gt;// 不推荐:l 易与 1 混淆&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="400" x="440" y="433" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=#DDD6FE;align=center;verticalAlign=middle;fontSize=11;fontColor=#5B21B6;rounded=1;spacingLeft=5;spacingRight=5;" value="💡 超过 int 范围(约21亿)的数字必须加 L 后缀" vertex="1">
          <mxGeometry height="24" width="300" x="440" y="468" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=#0EA5E9;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="数字下划线 &lt;font color=&quot;#ef4444&quot;&gt;(JDK 7+)&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="160" x="40" y="520" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0F9FF;strokeColor=#0EA5E9;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="200" width="820" x="40" y="560" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#FFFFFF;" value="✅ 合法位置" vertex="1">
          <mxGeometry height="25" width="360" x="60" y="575" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;int million = 1&lt;b&gt;_&lt;/b&gt;000&lt;b&gt;_&lt;/b&gt;000;&lt;/font&gt;&lt;font color=&quot;#64748b&quot;&gt;// 一百万&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="360" x="60" y="605" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;long card = 1234&lt;b&gt;_&lt;/b&gt;5678&lt;b&gt;_&lt;/b&gt;9012&lt;b&gt;_&lt;/b&gt;3456L;&lt;/font&gt;  &lt;font color=&quot;#64748b&quot;&gt;// 信用卡号&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="360" x="60" y="633" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;int bin = 0b1010&lt;b&gt;_&lt;/b&gt;0101&lt;b&gt;_&lt;/b&gt;1100&lt;b&gt;_&lt;/b&gt;0011;&lt;/font&gt;   &lt;font color=&quot;#64748b&quot;&gt;// 二进制&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="360" x="60" y="661" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#FFFFFF;" value="❌ 非法位置" vertex="1">
          <mxGeometry height="25" width="400" x="440" y="575" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=1;fontSize=11;fontColor=#334155;align=center;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;_&lt;/b&gt;100&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="80" x="440" y="605" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#EF4444;" value="开头" vertex="1">
          <mxGeometry height="28" width="40" x="520" y="605" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=1;fontSize=11;fontColor=#334155;align=center;" value="&lt;font face=&quot;Courier New&quot;&gt;100&lt;b&gt;_&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="80" x="570" y="605" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#EF4444;" value="结尾" vertex="1">
          <mxGeometry height="28" width="40" x="650" y="605" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=1;fontSize=11;fontColor=#334155;align=center;" value="&lt;font face=&quot;Courier New&quot;&gt;0&lt;b&gt;_&lt;/b&gt;x64&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="80" x="700" y="605" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#EF4444;" value="进制标识中" vertex="1">
          <mxGeometry height="28" width="60" x="780" y="605" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=1;fontSize=11;fontColor=#334155;align=center;" value="&lt;font face=&quot;Courier New&quot;&gt;3&lt;b&gt;_&lt;/b&gt;.14&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="80" x="440" y="643" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=1;fontSize=11;fontColor=#334155;align=center;" value="&lt;font face=&quot;Courier New&quot;&gt;3.&lt;b&gt;_&lt;/b&gt;14&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="80" x="570" y="643" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#EF4444;" value="小数点旁边" vertex="1">
          <mxGeometry height="28" width="50" x="520" y="643" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=1;fontSize=11;fontColor=#334155;align=center;" value="&lt;font face=&quot;Courier New&quot;&gt;1.0&lt;b&gt;_&lt;/b&gt;e10&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="80" x="700" y="643" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#EF4444;" value="指数符号旁" vertex="1">
          <mxGeometry height="28" width="60" x="780" y="643" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=#E0F2FE;align=center;verticalAlign=middle;fontSize=11;fontColor=#0369A1;rounded=1;spacingLeft=5;spacingRight=5;" value="💡 下划线只能放在数字之间,用于提高大数字的可读性" vertex="1">
          <mxGeometry height="24" width="340" x="60" y="700" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#94A3B8;strokeWidth=2;dashed=1;" value="" vertex="1">
          <mxGeometry height="55" width="400" x="440" y="690" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="text;html=1;strokeColor=none;fillColor=#64748B;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="记忆口诀" vertex="1">
          <mxGeometry height="18" width="60" x="450" y="698" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#475569;" value="&lt;b&gt;0b&lt;/b&gt; 二进制 | &lt;b&gt;0&lt;/b&gt; 八进制 | &lt;b&gt;0x&lt;/b&gt; 十六进制 | &lt;b&gt;L&lt;/b&gt; 长整型 | &lt;b&gt;_&lt;/b&gt; 分隔符" vertex="1">
          <mxGeometry height="20" width="380" x="450" y="718" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

2.3 整数溢出

public class OverflowDemo {
    public static void main(String[] args) {
        // 最大值 + 1 = 最小值(溢出)
        int max = Integer.MAX_VALUE;  // 2147483647
        System.out.println("max = " + max);
        System.out.println("max + 1 = " + (max + 1));  // -2147483648
        
        // 最小值 - 1 = 最大值(下溢)
        int min = Integer.MIN_VALUE;  // -2147483648
        System.out.println("min = " + min);
        System.out.println("min - 1 = " + (min - 1));  // 2147483647
    }
}
<mxfile>
  <diagram id="integer-overflow" name="整数溢出图解">
    <mxGraphModel dx="610" dy="277" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#1E293B;" value="整数溢出图解" vertex="1">
          <mxGeometry height="40" width="300" x="300" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#F1F5F9;align=center;verticalAlign=middle;fontSize=14;fontColor=#475569;rounded=1;spacingLeft=10;spacingRight=10;" value="以 &lt;font face=&quot;Courier New&quot; color=&quot;#6366f1&quot;&gt;&lt;b&gt;byte&lt;/b&gt;&lt;/font&gt; 为例(-128 ~ 127)" vertex="1">
          <mxGeometry height="30" width="260" x="320" y="70" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=none;strokeColor=#6366F1;strokeWidth=8;" value="" vertex="1">
          <mxGeometry height="300" width="300" x="300" y="120" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EEF2FF;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="200" width="200" x="350" y="170" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#4F46E5;" value="数值范围环" vertex="1">
          <mxGeometry height="30" width="130" x="385" y="250" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#6366F1;" value="byte 类型" vertex="1">
          <mxGeometry height="20" width="100" x="400" y="280" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontSize=18;fontStyle=1;fontColor=#FFFFFF;" value="127" vertex="1">
          <mxGeometry height="40" width="70" x="610" y="250" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontSize=18;fontStyle=1;fontColor=#FFFFFF;" value="-128" vertex="1">
          <mxGeometry height="40" width="70" x="220" y="250" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="0" vertex="1">
          <mxGeometry height="30" width="50" x="425" y="110" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#16A34A;" value="MAX" vertex="1">
          <mxGeometry height="15" width="50" x="620" y="290" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#DC2626;" value="MIN" vertex="1">
          <mxGeometry height="15" width="50" x="230" y="290" as="geometry" />
        </mxCell>
        <mxCell id="13" edge="1" parent="1" source="8" style="curved=1;endArrow=classic;html=1;strokeWidth=3;strokeColor=#F59E0B;endFill=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" target="9" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <Array as="points">
              <mxPoint x="720" y="270" />
              <mxPoint x="750" y="350" />
              <mxPoint x="720" y="450" />
              <mxPoint x="450" y="480" />
              <mxPoint x="180" y="450" />
              <mxPoint x="150" y="350" />
              <mxPoint x="180" y="270" />
            </Array>
            <mxPoint x="700" y="270" as="sourcePoint" />
            <mxPoint x="220" y="270" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=14;fontColor=#92400E;rounded=1;spacingLeft=10;spacingRight=10;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;127 + 1 = -128&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="170" x="365" y="430" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#DC2626;rounded=1;" value="溢出!" vertex="1">
          <mxGeometry height="20" width="50" x="430" y="475" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="64" vertex="1">
          <mxGeometry height="20" width="35" x="555" y="145" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="-64" vertex="1">
          <mxGeometry height="20" width="35" x="310" y="145" as="geometry" />
        </mxCell>
        <mxCell id="18" 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="140" x="40" y="520" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#8B5CF6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="120" width="820" x="40" y="560" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=right;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#475569;fontFamily=Courier New;" value="127:" vertex="1">
          <mxGeometry height="25" width="60" x="60" y="575" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#86EFAC;strokeWidth=1;fontSize=16;fontStyle=1;fontColor=#166534;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="130" y="575" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontStyle=1;fontColor=#334155;fontFamily=Courier New;" value="1" vertex="1">
          <mxGeometry height="30" width="35" x="165" y="575" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontStyle=1;fontColor=#334155;fontFamily=Courier New;" value="1" vertex="1">
          <mxGeometry height="30" width="35" x="200" y="575" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontStyle=1;fontColor=#334155;fontFamily=Courier New;" value="1" vertex="1">
          <mxGeometry height="30" width="35" x="235" y="575" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontStyle=1;fontColor=#334155;fontFamily=Courier New;" value="1" vertex="1">
          <mxGeometry height="30" width="35" x="280" y="575" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontStyle=1;fontColor=#334155;fontFamily=Courier New;" value="1" vertex="1">
          <mxGeometry height="30" width="35" x="315" y="575" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontStyle=1;fontColor=#334155;fontFamily=Courier New;" value="1" vertex="1">
          <mxGeometry height="30" width="35" x="350" y="575" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontStyle=1;fontColor=#334155;fontFamily=Courier New;" value="1" vertex="1">
          <mxGeometry height="30" width="35" x="385" y="575" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=right;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#475569;fontFamily=Courier New;" value="+ 1:" vertex="1">
          <mxGeometry height="25" width="60" x="60" y="605" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="130" y="605" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="165" y="605" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="200" y="605" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="235" y="605" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="280" y="605" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="315" y="605" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="350" y="605" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#93C5FD;strokeWidth=1;fontSize=16;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="1" vertex="1">
          <mxGeometry height="30" width="35" x="385" y="605" as="geometry" />
        </mxCell>
        <mxCell id="38" edge="1" parent="1" style="endArrow=none;html=1;strokeWidth=2;strokeColor=#64748B;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="130" y="640" as="sourcePoint" />
            <mxPoint x="420" y="640" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=right;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#475569;fontFamily=Courier New;" value="结果:" vertex="1">
          <mxGeometry height="25" width="60" x="60" y="645" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#FECACA;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#DC2626;fontFamily=Courier New;" value="1" vertex="1">
          <mxGeometry height="30" width="35" x="130" y="645" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="165" y="645" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="200" y="645" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="235" y="645" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="280" y="645" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="315" y="645" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="350" y="645" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F1F5F9;strokeColor=#CBD5E1;strokeWidth=1;fontSize=16;fontColor=#94A3B8;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="30" width="35" x="385" y="645" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#DC2626;" value="符号位" vertex="1">
          <mxGeometry height="20" width="45" x="125" y="555" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#8B5CF6;" value="→" vertex="1">
          <mxGeometry height="40" width="40" x="430" y="605" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=2;fontSize=24;fontStyle=1;fontColor=#FFFFFF;" value="-128" vertex="1">
          <mxGeometry height="50" width="100" x="480" y="600" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF2F2;align=center;verticalAlign=middle;fontSize=11;fontColor=#991B1B;rounded=1;" value="最高位进位&lt;br&gt;变成符号位" vertex="1">
          <mxGeometry height="40" width="80" x="590" y="605" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#EF4444;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="160" x="690" y="570" as="geometry" />
        </mxCell>
        <mxCell id="54" 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="20" width="60" x="700" y="580" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#991B1B;" value="Java 不会自动检测溢出&lt;br&gt;需要程序员自行处理!" vertex="1">
          <mxGeometry height="50" width="150" x="695" y="610" as="geometry" />
        </mxCell>
        <mxCell id="56" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#166534;rounded=1;" value="正数" vertex="1">
          <mxGeometry height="15" width="35" x="130" y="595" as="geometry" />
        </mxCell>
        <mxCell id="57" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#991B1B;rounded=1;" value="负数" vertex="1">
          <mxGeometry height="15" width="35" x="130" y="665" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

安全的算术运算

// JDK 8+ 提供安全的算术方法,溢出时抛出异常
public class SafeMathDemo {
    public static void main(String[] args) {
        int a = Integer.MAX_VALUE;
        int b = 1;
        
        // 普通加法(静默溢出)
        int result1 = a + b;  // -2147483648,无任何提示
        
        // 安全加法(溢出时抛出异常)
        try {
            int result2 = Math.addExact(a, b);  // 抛出 ArithmeticException
        } catch (ArithmeticException e) {
            System.out.println("溢出!" + e.getMessage());
        }
        
        // 其他安全方法
        Math.subtractExact(a, b);  // 安全减法
        Math.multiplyExact(a, b);  // 安全乘法
        Math.negateExact(a);       // 安全取负
        Math.incrementExact(a);    // 安全自增
        Math.decrementExact(a);    // 安全自减
    }
}

三、浮点类型详解

3.1 IEEE 754 标准

<mxfile>
  <diagram id="ieee754-float" name="IEEE 754 浮点数表示">
    <mxGraphModel dx="1307" dy="1153" 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="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=22;fontStyle=1;fontColor=#1a237e;" value="IEEE 754 浮点数表示" vertex="1">
          <mxGeometry height="40" width="500" x="200" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#5c6bc0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="700" x="100" y="70" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontStyle=1;" value="浮点数 = &lt;font color=&quot;#c62828&quot;&gt;(-1)&lt;sup&gt;符号位&lt;/sup&gt;&lt;/font&gt; × &lt;font color=&quot;#2e7d32&quot;&gt;1.尾数&lt;/font&gt; × &lt;font color=&quot;#1565c0&quot;&gt;2&lt;sup&gt;(指数-偏移量)&lt;/sup&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="700" x="100" y="70" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontStyle=1;fontColor=#e65100;" value="float (32 位)" vertex="1">
          <mxGeometry height="30" width="150" x="140" y="140" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#c62828;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="60" width="50" x="140" y="175" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="&lt;b&gt;S&lt;/b&gt;&lt;br&gt;1位" vertex="1">
          <mxGeometry height="60" width="50" x="140" y="175" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#c62828;" value="符号" vertex="1">
          <mxGeometry height="20" width="50" x="140" y="235" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#388e3c;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="60" width="150" x="195" y="175" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="&lt;b&gt;Exponent&lt;/b&gt;&lt;br&gt;8 位" vertex="1">
          <mxGeometry height="60" width="150" x="195" y="175" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#388e3c;" value="指数 (偏移127)" vertex="1">
          <mxGeometry height="20" width="150" x="195" y="235" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="60" width="350" x="350" y="175" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="&lt;b&gt;Mantissa&lt;/b&gt;&lt;br&gt;23 位" vertex="1">
          <mxGeometry height="60" width="350" x="350" y="175" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#1976d2;" value="尾数" vertex="1">
          <mxGeometry height="20" width="100" x="475" y="235" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontStyle=2;fontColor=#666666;" value="32位" vertex="1">
          <mxGeometry height="30" width="40" x="720" y="190" as="geometry" />
        </mxCell>
        <mxCell id="16" edge="1" parent="1" style="endArrow=none;html=1;strokeColor=#666666;strokeWidth=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="710" y="175" as="sourcePoint" />
            <mxPoint x="710" y="235" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontStyle=1;fontColor=#6a1b9a;" value="double (64 位)" vertex="1">
          <mxGeometry height="30" width="150" x="140" y="270" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=#7b1fa2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="60" width="50" x="140" y="305" 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=12;" value="&lt;b&gt;S&lt;/b&gt;&lt;br&gt;1位" vertex="1">
          <mxGeometry height="60" width="50" x="140" y="305" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#7b1fa2;" value="符号" vertex="1">
          <mxGeometry height="20" width="50" x="140" y="365" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#dcedc8;strokeColor=#689f38;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="60" width="180" x="195" y="305" 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=12;" value="&lt;b&gt;Exponent&lt;/b&gt;&lt;br&gt;11 位" vertex="1">
          <mxGeometry height="60" width="180" x="195" y="305" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#689f38;" value="指数 (偏移1023)" vertex="1">
          <mxGeometry height="20" width="180" x="195" y="365" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e1f5fe;strokeColor=#0288d1;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="60" width="320" x="380" y="305" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="&lt;b&gt;Mantissa&lt;/b&gt;&lt;br&gt;52 位" vertex="1">
          <mxGeometry height="60" width="320" x="380" y="305" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#0288d1;" value="尾数" vertex="1">
          <mxGeometry height="20" width="100" x="490" y="365" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontStyle=2;fontColor=#666666;" value="64位" vertex="1">
          <mxGeometry height="30" width="40" x="720" y="320" as="geometry" />
        </mxCell>
        <mxCell id="28" edge="1" parent="1" style="endArrow=none;html=1;strokeColor=#666666;strokeWidth=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="710" y="305" as="sourcePoint" />
            <mxPoint x="710" y="365" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontStyle=1;fontColor=#00695c;" value="📝 示例:float 表示 6.5" vertex="1">
          <mxGeometry height="30" width="250" x="100" y="405" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e0f2f1;strokeColor=#00897b;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="280" width="700" x="100" y="440" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#26a69a;" value="" vertex="1">
          <mxGeometry height="55" width="300" x="120" y="455" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="&lt;b&gt;Step 1: 十进制转二进制&lt;/b&gt;&lt;br&gt;6.5 = &lt;font face=&quot;Courier New&quot;&gt;110.1&lt;/font&gt; (二进制)" vertex="1">
          <mxGeometry height="55" width="300" x="120" y="455" as="geometry" />
        </mxCell>
        <mxCell id="33" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#00897b;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="425" y="482" as="sourcePoint" />
            <mxPoint x="475" y="482" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#26a69a;" value="" vertex="1">
          <mxGeometry height="55" width="300" x="480" y="455" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="&lt;b&gt;Step 2: 规格化&lt;/b&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot;&gt;110.1&lt;/font&gt; = &lt;font face=&quot;Courier New&quot;&gt;1.101&lt;/font&gt; × 2²" vertex="1">
          <mxGeometry height="55" width="300" x="480" y="455" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#e57373;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="120" y="525" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="&lt;b&gt;符号位&lt;/b&gt;: &lt;font face=&quot;Courier New&quot; color=&quot;#c62828&quot;&gt;0&lt;/font&gt; (正数)" vertex="1">
          <mxGeometry height="50" width="200" x="120" y="525" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#81c784;" value="" vertex="1">
          <mxGeometry height="50" width="200" x="340" y="525" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="&lt;b&gt;指数&lt;/b&gt;: 2 + 127 = 129&lt;br&gt;= &lt;font face=&quot;Courier New&quot; color=&quot;#388e3c&quot;&gt;1000 0001&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="200" x="340" y="525" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#64b5f6;" value="" vertex="1">
          <mxGeometry height="50" width="220" x="560" y="525" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="&lt;b&gt;尾数&lt;/b&gt; (省略前导1):&lt;br&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#1976d2&quot;&gt;101 0000...0000&lt;/font&gt; (23位)" vertex="1">
          <mxGeometry height="50" width="220" x="560" y="525" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontStyle=1;fontColor=#00695c;" value="完整的 32 位表示:" vertex="1">
          <mxGeometry height="25" width="150" x="120" y="590" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#c62828;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="40" x="120" y="620" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;0&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="40" x="120" y="620" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#388e3c;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="150" x="165" y="620" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;10000001&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="150" x="165" y="620" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#bbdefb;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="460" x="320" y="620" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;10100000000000000000000&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="460" x="320" y="620" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=9;fontColor=#c62828;" value="1位" vertex="1">
          <mxGeometry height="20" width="40" x="120" y="665" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=9;fontColor=#388e3c;" value="8位" vertex="1">
          <mxGeometry height="20" width="80" x="200" y="665" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=9;fontColor=#1976d2;" value="23位" vertex="1">
          <mxGeometry height="20" width="100" x="500" y="665" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="十六进制: &lt;font face=&quot;Courier New&quot; color=&quot;#6a1b9a&quot;&gt;&lt;b&gt;0x40D00000&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="250" x="120" y="690" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

3.2 浮点数精度问题

public class FloatPrecisionDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // 经典问题:0.1 + 0.2 ≠ 0.3
        // ═══════════════════════════════════════════════════════════════
        
        double a = 0.1;
        double b = 0.2;
        double c = 0.3;
        
        System.out.println("0.1 + 0.2 = " + (a + b));  // 0.30000000000000004
        System.out.println("0.1 + 0.2 == 0.3 ? " + (a + b == c));  // false
        
        // ═══════════════════════════════════════════════════════════════
        // 另一个例子
        // ═══════════════════════════════════════════════════════════════
        
        float f1 = 0.3f - 0.2f;  // 0.100000024
        float f2 = 0.2f - 0.1f;  // 0.099999905
        System.out.println("f1 = " + f1);
        System.out.println("f2 = " + f2);
        System.out.println("f1 == f2 ? " + (f1 == f2));  // false
    }
}
<mxfile>
  <diagram id="float-precision" name="为什么0.1+0.2≠0.3">
    <mxGraphModel dx="1068" dy="484" 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="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#1E293B;" value="为什么 0.1 + 0.2 ≠ 0.3 ?" vertex="1">
          <mxGeometry height="45" width="450" x="225" y="15" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;" value="IEEE 754 浮点数精度问题详解" vertex="1">
          <mxGeometry height="25" width="250" x="325" y="55" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Step 1: 十进制转二进制" vertex="1">
          <mxGeometry height="30" width="180" x="40" y="95" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="250" width="400" x="40" y="135" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;spacingLeft=5;" value="十进制 0.1 转换为二进制的过程:" vertex="1">
          <mxGeometry height="20" width="220" x="50" y="145" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;0.1 × 2 = 0.2&lt;/font&gt;" vertex="1">
          <mxGeometry height="24" width="140" x="60" y="170" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="→ 0" vertex="1">
          <mxGeometry height="24" width="40" x="210" y="170" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;0.2 × 2 = 0.4&lt;/font&gt;" vertex="1">
          <mxGeometry height="24" width="140" x="60" y="194" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="→ 0" vertex="1">
          <mxGeometry height="24" width="40" x="210" y="194" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;0.4 × 2 = 0.8&lt;/font&gt;" vertex="1">
          <mxGeometry height="24" width="140" x="60" y="218" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="→ 0" vertex="1">
          <mxGeometry height="24" width="40" x="210" y="218" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;0.8 × 2 = 1.6&lt;/font&gt;" vertex="1">
          <mxGeometry height="24" width="140" x="60" y="242" as="geometry" />
        </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="→ 1" vertex="1">
          <mxGeometry height="24" width="40" x="210" y="242" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;0.6 × 2 = 1.2&lt;/font&gt;" vertex="1">
          <mxGeometry height="24" width="140" x="60" y="266" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#DC2626;rounded=1;" value="→ 1" vertex="1">
          <mxGeometry height="24" width="40" x="210" y="266" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#F59E0B;strokeWidth=2;fontSize=11;fontColor=#334155;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;0.2 × 2 = 0.4&lt;/font&gt;" vertex="1">
          <mxGeometry height="24" width="140" x="60" y="290" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="→ 0" vertex="1">
          <mxGeometry height="24" width="40" x="210" y="290" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="← 开始循环!" vertex="1">
          <mxGeometry height="24" width="80" x="260" y="290" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontColor=#64748B;" value="&lt;font face=&quot;Courier New&quot;&gt;...&lt;/font&gt;" vertex="1">
          <mxGeometry height="20" width="40" x="60" y="314" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#EF4444;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="35" width="380" x="50" y="340" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=13;fontColor=#991B1B;" value="0.1₁₀ = &lt;font face=&quot;Courier New&quot;&gt;0.&lt;b&gt;0001100110011&lt;/b&gt;0011...&lt;/font&gt;₂" vertex="1">
          <mxGeometry height="25" width="300" x="55" y="345" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="无限循环!" vertex="1">
          <mxGeometry height="18" width="65" x="345" y="348.5" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Step 2: 存储截断" vertex="1">
          <mxGeometry height="30" width="140" x="460" y="95" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#8B5CF6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="250" width="400" x="460" y="135" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#6D28D9;spacingLeft=5;" value="浮点数位数有限,必须截断:" vertex="1">
          <mxGeometry height="20" width="200" x="470" y="145" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#DDD6FE;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="80" width="380" x="470" y="170" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="double 类型(64位)" vertex="1">
          <mxGeometry height="20" width="120" x="480" y="178" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#FECACA;strokeWidth=1;fontSize=10;fontStyle=1;fontColor=#991B1B;" value="符号位" vertex="1">
          <mxGeometry height="35" width="40" x="480" y="205" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=9;fontColor=#991B1B;" value="1位" vertex="1">
          <mxGeometry height="15" width="40" x="480" y="225" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#BBF7D0;strokeWidth=1;fontSize=10;fontStyle=1;fontColor=#166534;" value="指数部分" vertex="1">
          <mxGeometry height="35" width="100" x="520" y="205" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=9;fontColor=#166534;" value="11位" vertex="1">
          <mxGeometry height="15" width="100" x="520" y="225" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#BFDBFE;strokeWidth=1;fontSize=10;fontStyle=1;fontColor=#1E40AF;" value="尾数部分(精度关键)" vertex="1">
          <mxGeometry height="35" width="220" x="620" y="205" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=9;fontColor=#1E40AF;" value="52位" vertex="1">
          <mxGeometry height="15" width="220" x="620" y="225" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#6D28D9;spacingLeft=5;" value="实际存储值(近似值):" vertex="1">
          <mxGeometry height="20" width="150" x="470" y="260" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#F3E8FF;align=right;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#7C3AED;rounded=1;spacingRight=5;" value="0.1 ≈" vertex="1">
          <mxGeometry height="22" width="45" x="470" y="285" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#F8FAFC;align=left;verticalAlign=middle;fontSize=10;fontColor=#475569;rounded=1;spacingLeft=5;" value="&lt;font face=&quot;Courier New&quot;&gt;0.100000000000000005551...&lt;/font&gt;" vertex="1">
          <mxGeometry height="22" width="320" x="520" y="285" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=#F3E8FF;align=right;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#7C3AED;rounded=1;spacingRight=5;" value="0.2 ≈" vertex="1">
          <mxGeometry height="22" width="45" x="470" y="312" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=#F8FAFC;align=left;verticalAlign=middle;fontSize=10;fontColor=#475569;rounded=1;spacingLeft=5;" value="&lt;font face=&quot;Courier New&quot;&gt;0.200000000000000011102...&lt;/font&gt;" vertex="1">
          <mxGeometry height="22" width="320" x="520" y="312" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=right;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#DC2626;rounded=1;spacingRight=5;" value="相加 ≈" vertex="1">
          <mxGeometry height="22" width="45" x="470" y="339" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF2F2;align=left;verticalAlign=middle;fontSize=10;fontColor=#991B1B;rounded=1;spacingLeft=5;" value="&lt;font face=&quot;Courier New&quot;&gt;0.300000000000000044408...&lt;/font&gt;" vertex="1">
          <mxGeometry height="22" width="320" x="520" y="339" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Step 3: 最终结果" vertex="1">
          <mxGeometry height="30" width="140" x="40" y="400" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#EF4444;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="400" x="40" y="440" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#991B1B;spacingLeft=5;" value="Java 控制台输出:" vertex="1">
          <mxGeometry height="20" width="140" x="50" y="450" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=12;fontColor=#22C55E;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;System.out.println(0.1 + 0.2);&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="280" x="50" y="475" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=12;fontColor=#F59E0B;align=left;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;// 输出: &lt;b&gt;0.30000000000000004&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="280" x="50" y="503" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="≠ 0.3" vertex="1">
          <mxGeometry height="35" width="80" x="345" y="485" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="这不是 Bug!" vertex="1">
          <mxGeometry height="30" width="120" x="460" y="400" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="100" width="400" x="460" y="440" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#92400E;spacingLeft=5;" value="⚠️ IEEE 754 标准的固有限制" vertex="1">
          <mxGeometry height="20" width="220" x="470" y="450" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#78350F;spacingLeft=5;" value="所有遵循此标准的语言都有这个问题:" vertex="1">
          <mxGeometry height="20" width="250" x="470" y="475" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="Java" vertex="1">
          <mxGeometry height="25" width="50" x="480" y="500" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="C/C++" vertex="1">
          <mxGeometry height="25" width="55" x="540" y="500" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="JavaScript" vertex="1">
          <mxGeometry height="25" width="75" x="605" y="500" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="Python" vertex="1">
          <mxGeometry height="25" width="55" x="690" y="500" as="geometry" />
        </mxCell>
        <mxCell id="56" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#78350F;" value="..." vertex="1">
          <mxGeometry height="25" width="30" x="755" y="500" as="geometry" />
        </mxCell>
        <mxCell id="57" 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="100" x="40" y="555" as="geometry" />
        </mxCell>
        <mxCell id="58" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="90" width="820" x="40" y="595" as="geometry" />
        </mxCell>
        <mxCell id="59" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#FFFFFF;" value="1. 使用 BigDecimal" vertex="1">
          <mxGeometry height="25" width="140" x="55" y="610" as="geometry" />
        </mxCell>
        <mxCell id="60" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=10;fontColor=#334155;align=left;spacingLeft=5;" value="&lt;font face=&quot;Courier New&quot;&gt;new BigDecimal(&quot;0.1&quot;).add(new BigDecimal(&quot;0.2&quot;))&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="340" x="54" y="643" as="geometry" />
        </mxCell>
        <mxCell id="61" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#FFFFFF;" value="2. 误差容忍比较" vertex="1">
          <mxGeometry height="25" width="130" x="420" y="610" as="geometry" />
        </mxCell>
        <mxCell id="62" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=10;fontColor=#334155;align=left;spacingLeft=5;" value="&lt;font face=&quot;Courier New&quot;&gt;Math.abs(a - b) &amp;lt; 1e-10&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="180" x="419" y="643" as="geometry" />
        </mxCell>
        <mxCell id="63" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#8B5CF6;strokeColor=#7C3AED;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#FFFFFF;" value="3. 整数运算" vertex="1">
          <mxGeometry height="25" width="100" x="625" y="610" as="geometry" />
        </mxCell>
        <mxCell id="64" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#CBD5E1;strokeWidth=1;fontSize=10;fontColor=#334155;align=left;spacingLeft=5;" value="&lt;font face=&quot;Courier New&quot;&gt;用分表示: 10 + 20 = 30&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="180" x="624" y="643" as="geometry" />
        </mxCell>
        <mxCell id="65" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=20;" value="💡" vertex="1">
          <mxGeometry height="30" width="30" x="810" y="605" as="geometry" />
        </mxCell>
        <mxCell id="66" edge="1" parent="1" source="23" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" target="23">
          <mxGeometry relative="1" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

3.3 特殊浮点值

public class SpecialFloatDemo {
    public static void main(String[] args) {
        // 正无穷大
        double posInf = Double.POSITIVE_INFINITY;
        System.out.println("1.0 / 0.0 = " + (1.0 / 0.0));  // Infinity
        System.out.println("Max * 2 = " + (Double.MAX_VALUE * 2));  // Infinity
        
        // 负无穷大
        double negInf = Double.NEGATIVE_INFINITY;
        System.out.println("-1.0 / 0.0 = " + (-1.0 / 0.0));  // -Infinity
        
        // NaN (Not a Number)
        double nan = Double.NaN;
        System.out.println("0.0 / 0.0 = " + (0.0 / 0.0));  // NaN
        System.out.println("sqrt(-1) = " + Math.sqrt(-1));  // NaN
        
        // NaN 的特殊性质
        System.out.println("NaN == NaN ? " + (nan == nan));  // false!
        System.out.println("NaN != NaN ? " + (nan != nan));  // true!
        
        // 正确判断 NaN
        System.out.println("isNaN ? " + Double.isNaN(nan));  // true
        
        // 判断无穷大
        System.out.println("isInfinite ? " + Double.isInfinite(posInf));  // true
        System.out.println("isFinite ? " + Double.isFinite(1.0));  // true
    }
}
<mxfile>
  <diagram id="special-float-values" name="特殊浮点值对比">
    <mxGraphModel dx="926" dy="817" 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="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;fontStyle=1;fontColor=#1a237e;" value="特殊浮点值对比" vertex="1">
          <mxGeometry height="45" width="400" x="250" y="25" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#5c6bc0;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="360" width="790" x="50" y="70" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#3949ab;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="50" width="740" x="80" y="85" 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=16;fontStyle=1;fontColor=#ffffff;" value="值" vertex="1">
          <mxGeometry height="50" width="180" x="80" y="85" 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=16;fontStyle=1;fontColor=#ffffff;" value="产生条件" vertex="1">
          <mxGeometry height="50" width="560" x="260" y="85" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="line;strokeWidth=2;html=1;strokeColor=#5c6bc0;" value="" vertex="1">
          <mxGeometry height="1" width="740" x="80" y="135" as="geometry" />
        </mxCell>
        <mxCell id="8" edge="1" parent="1" style="endArrow=none;html=1;strokeColor=#e0e0e0;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="260" y="135" as="sourcePoint" />
            <mxPoint x="260" y="425" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="70" width="740" x="80" y="136" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="40" width="140" x="100" y="151" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#ffffff;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;Infinity&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="140" x="100" y="151" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#1565c0&quot;&gt;正数 / 0.0&lt;/font&gt;,溢出到正无穷" vertex="1">
          <mxGeometry height="70" width="520" x="280" y="136" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;fontColor=#1976d2;fontStyle=1;" value="∞" vertex="1">
          <mxGeometry height="70" width="50" x="750" y="136" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="line;strokeWidth=1;html=1;strokeColor=#e0e0e0;" value="" vertex="1">
          <mxGeometry height="1" width="740" x="80" y="206" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="70" width="740" x="80" y="207" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c62828;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="40" width="140" x="100" y="222" 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=15;fontColor=#ffffff;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;-Infinity&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="140" x="100" y="222" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#c62828&quot;&gt;负数 / 0.0&lt;/font&gt;,溢出到负无穷" vertex="1">
          <mxGeometry height="70" width="520" x="280" y="207" 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=24;fontColor=#c62828;fontStyle=1;" value="-∞" vertex="1">
          <mxGeometry height="70" width="50" x="750" y="207" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="line;strokeWidth=1;html=1;strokeColor=#e0e0e0;" value="" vertex="1">
          <mxGeometry height="1" width="740" x="80" y="277" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="70" width="740" x="80" y="278" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ef6c00;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="40" width="140" x="100" y="293" 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=15;fontColor=#ffffff;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;NaN&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="140" x="100" y="293" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#e65100&quot;&gt;0.0 / 0.0&lt;/font&gt;,&lt;font face=&quot;Courier New&quot; color=&quot;#e65100&quot;&gt;√负数&lt;/font&gt;,无意义运算" vertex="1">
          <mxGeometry height="70" width="520" x="280" y="278" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;fontColor=#ef6c00;fontStyle=1;" value="?" vertex="1">
          <mxGeometry height="70" width="50" x="750" y="278" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="line;strokeWidth=1;html=1;strokeColor=#e0e0e0;" value="" vertex="1">
          <mxGeometry height="1" width="740" x="80" y="348" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="70" width="740" x="80" y="349" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7b1fa2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="40" width="140" x="100" y="364" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#ffffff;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;-0.0&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="140" x="100" y="364" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="负数下溢,与 &lt;font face=&quot;Courier New&quot; color=&quot;#7b1fa2&quot;&gt;+0.0&lt;/font&gt; 相等但符号不同" vertex="1">
          <mxGeometry height="70" width="520" x="280" y="349" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;fontColor=#7b1fa2;fontStyle=1;" value="±" vertex="1">
          <mxGeometry height="70" width="50" x="750" y="349" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff8e1;strokeColor=#ffa000;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="100" width="740" x="80" y="445" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=32;" value="⚠️" vertex="1">
          <mxGeometry height="60" width="60" x="100" y="465" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontStyle=1;fontColor=#e65100;" value="注意" vertex="1">
          <mxGeometry height="30" width="60" x="160" y="455" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffccbc;strokeColor=#ff7043;" value="" vertex="1">
          <mxGeometry height="45" width="310" x="160" y="485" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="&lt;b&gt;整数&lt;/b&gt;除以 0 → 抛出 &lt;font face=&quot;Courier New&quot; color=&quot;#d84315&quot;&gt;ArithmeticException&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="310" x="160" y="485" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#66bb6a;" value="" vertex="1">
          <mxGeometry height="45" width="310" x="490" y="485" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;" value="&lt;b&gt;浮点数&lt;/b&gt;除以 0 → 得到 &lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;Infinity&lt;/font&gt; 或 &lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;NaN&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="310" x="490" y="485" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#5c6bc0;strokeColor=none;fontColor=#ffffff;fontStyle=1;fontSize=11;" value="VS" vertex="1">
          <mxGeometry height="25" width="35" x="460" y="495" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontStyle=2;fontColor=#9e9e9e;" value="IEEE 754 标准定义了这些特殊值的表示方式" vertex="1">
          <mxGeometry height="25" width="400" x="250" y="560" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

四、字符类型

4.1 char 类型详解

<mxfile>
  <diagram id="char-type" name="char类型详解">
    <mxGraphModel dx="1281" dy="581" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;fontStyle=1;fontColor=#1E293B;" value="char 类型" vertex="1">
          <mxGeometry height="45" width="250" x="305" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;" value="Java 字符类型详解" vertex="1">
          <mxGeometry height="25" width="170" x="345" y="60" as="geometry" />
        </mxCell>
        <mxCell id="4" 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="40" y="105" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="200" width="380" x="40" y="145" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BFDBFE;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="70" width="165" x="55" y="160" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#1E40AF;" value="📦 存储大小" vertex="1">
          <mxGeometry height="20" width="80" x="60" y="165" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="2 字节" vertex="1">
          <mxGeometry height="30" width="70" x="65" y="188" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;rounded=1;" value="16 位" vertex="1">
          <mxGeometry height="22" width="50" x="145" y="193" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BFDBFE;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="70" width="170" x="235" y="160" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#1E40AF;" value="🌐 编码方式" vertex="1">
          <mxGeometry height="20" width="80" x="240" y="165" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Unicode" vertex="1">
          <mxGeometry height="30" width="90" x="250" y="188" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#F3E8FF;align=center;verticalAlign=middle;fontSize=10;fontColor=#7C3AED;rounded=1;" value="UTF-16" vertex="1">
          <mxGeometry height="22" width="50" x="345" y="193" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BFDBFE;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="85" width="165" x="55" y="245" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#1E40AF;" value="📊 取值范围" vertex="1">
          <mxGeometry height="20" width="80" x="60" y="250" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;fontFamily=Courier New;" value="0" vertex="1">
          <mxGeometry height="28" width="50" x="65" y="275" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#64748B;" value="~" vertex="1">
          <mxGeometry height="28" width="25" x="115" y="275" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;fontFamily=Courier New;" value="65535" vertex="1">
          <mxGeometry height="28" width="70" x="140" y="275" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#92400E;rounded=1;" value="无符号整数" vertex="1">
          <mxGeometry height="18" width="70" x="80" y="308" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BFDBFE;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="85" width="170" x="235" y="245" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#1E40AF;" value="🔢 算术运算" vertex="1">
          <mxGeometry height="20" width="80" x="240" y="250" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="可参与运算" vertex="1">
          <mxGeometry height="25" width="90" x="250" y="275" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=10;fontColor=#166534;rounded=1;" value="自动转为 int" vertex="1">
          <mxGeometry height="20" width="80" x="255" y="305" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="char 的各种表示方式" vertex="1">
          <mxGeometry height="30" width="170" x="440" y="105" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="215" width="420" x="440" y="145" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FDE68A;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="390" x="455" y="157" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=16;fontColor=#92400E;rounded=1;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;&#39;A&#39;&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="26" width="60" x="465" y="165" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="字符字面量" vertex="1">
          <mxGeometry height="22" width="80" x="540" y="167" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#78350F;" value="最常用的方式,直接用单引号包裹字符" vertex="1">
          <mxGeometry height="22" width="210" x="630" y="167" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FDE68A;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="390" x="455" y="207" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=#F3E8FF;align=center;verticalAlign=middle;fontSize=14;fontColor=#7C3AED;rounded=1;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;&#39;\u0041&#39;&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="26" width="80" x="465" y="215" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Unicode 转义" vertex="1">
          <mxGeometry height="22" width="90" x="555" y="217" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#78350F;" value="等于 &#39;A&#39;,\u + 4位十六进制" vertex="1">
          <mxGeometry height="22" width="180" x="655" y="217" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FDE68A;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="390" x="455" y="257" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=16;fontColor=#1E40AF;rounded=1;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;65&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="26" width="60" x="465" y="265" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="整数值" vertex="1">
          <mxGeometry height="22" width="60" x="540" y="267" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#78350F;" value="等于 &#39;A&#39;,直接使用 Unicode 码值" vertex="1">
          <mxGeometry height="22" width="220" x="610" y="267" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FDE68A;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="390" x="455" y="307" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=16;fontColor=#166534;rounded=1;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;&#39;\n&#39;&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="26" width="60" x="465" y="315" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="转义字符" vertex="1">
          <mxGeometry height="22" width="70" x="540" y="317" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#78350F;" value="换行符,还有 \t \r \\ \&#39; 等" vertex="1">
          <mxGeometry height="22" width="200" x="620" y="317" as="geometry" />
        </mxCell>
        <mxCell id="42" 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="40" y="380" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="140" width="380" x="40" y="420" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#166534;" value="&lt;font face=&quot;Courier New&quot;&gt;\n&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="50" x="55" y="435" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#166534;" value="换行" vertex="1">
          <mxGeometry height="28" width="40" x="115" y="435" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#166534;" value="&lt;font face=&quot;Courier New&quot;&gt;\t&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="50" x="165" y="435" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#166534;" value="制表符" vertex="1">
          <mxGeometry height="28" width="50" x="225" y="435" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#166534;" value="&lt;font face=&quot;Courier New&quot;&gt;\r&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="50" x="285" y="435" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#166534;" value="回车" vertex="1">
          <mxGeometry height="28" width="40" x="345" y="435" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#166534;" value="&lt;font face=&quot;Courier New&quot;&gt;\\&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="50" x="55" y="475" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#166534;" value="反斜杠" vertex="1">
          <mxGeometry height="28" width="50" x="115" y="475" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#166534;" value="&lt;font face=&quot;Courier New&quot;&gt;\&#39;&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="50" x="165" y="475" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#166534;" value="单引号" vertex="1">
          <mxGeometry height="28" width="50" x="225" y="475" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#166534;" value="&lt;font face=&quot;Courier New&quot;&gt;\&quot;&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="50" x="285" y="475" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#166534;" value="双引号" vertex="1">
          <mxGeometry height="28" width="50" x="345" y="475" as="geometry" />
        </mxCell>
        <mxCell id="56" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#166534;" value="&lt;font face=&quot;Courier New&quot;&gt;\0&lt;/font&gt;" vertex="1">
          <mxGeometry height="28" width="50" x="55" y="515" as="geometry" />
        </mxCell>
        <mxCell id="57" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#166534;" value="空字符" vertex="1">
          <mxGeometry height="28" width="50" x="115" y="515" as="geometry" />
        </mxCell>
        <mxCell id="58" 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="120" x="440" y="380" as="geometry" />
        </mxCell>
        <mxCell id="59" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#8B5CF6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="140" width="260" x="440" y="420" as="geometry" />
        </mxCell>
        <mxCell id="60" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="字符" vertex="1">
          <mxGeometry height="22" width="50" x="455" y="430" as="geometry" />
        </mxCell>
        <mxCell id="61" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="十进制" vertex="1">
          <mxGeometry height="22" width="60" x="515" y="430" as="geometry" />
        </mxCell>
        <mxCell id="62" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="说明" vertex="1">
          <mxGeometry height="22" width="80" x="585" y="430" as="geometry" />
        </mxCell>
        <mxCell id="63" parent="1" style="text;html=1;strokeColor=none;fillColor=#F3E8FF;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#7C3AED;rounded=1;" value="&#39;0&#39;-&#39;9&#39;" vertex="1">
          <mxGeometry height="24" width="50" x="455" y="460" as="geometry" />
        </mxCell>
        <mxCell id="64" parent="1" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#6D28D9;rounded=1;strokeWidth=1;" value="48-57" vertex="1">
          <mxGeometry height="24" width="60" x="515" y="460" as="geometry" />
        </mxCell>
        <mxCell id="65" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#6D28D9;" value="数字字符" vertex="1">
          <mxGeometry height="24" width="80" x="585" y="460" as="geometry" />
        </mxCell>
        <mxCell id="66" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#1E40AF;rounded=1;" value="&#39;A&#39;-&#39;Z&#39;" vertex="1">
          <mxGeometry height="24" width="50" x="455" y="492" as="geometry" />
        </mxCell>
        <mxCell id="67" parent="1" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#1E40AF;rounded=1;strokeWidth=1;" value="65-90" vertex="1">
          <mxGeometry height="24" width="60" x="515" y="492" as="geometry" />
        </mxCell>
        <mxCell id="68" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#1E40AF;" value="大写字母" vertex="1">
          <mxGeometry height="24" width="80" x="585" y="492" as="geometry" />
        </mxCell>
        <mxCell id="69" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#166534;rounded=1;" value="&#39;a&#39;-&#39;z&#39;" vertex="1">
          <mxGeometry height="24" width="50" x="455" y="524" as="geometry" />
        </mxCell>
        <mxCell id="70" parent="1" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#166534;rounded=1;strokeWidth=1;" value="97-122" vertex="1">
          <mxGeometry height="24" width="60" x="515" y="524" as="geometry" />
        </mxCell>
        <mxCell id="71" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#166534;" value="小写字母" vertex="1">
          <mxGeometry height="24" width="80" x="585" y="524" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
public class CharDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // char 的各种写法
        // ═══════════════════════════════════════════════════════════════
        
        char c1 = 'A';           // 字符字面量
        char c2 = '\u0041';      // Unicode 转义
        char c3 = 65;            // 整数赋值
        char c4 = '\n';          // 转义字符
        
        System.out.println(c1 == c2);  // true
        System.out.println(c1 == c3);  // true
        
        // ═══════════════════════════════════════════════════════════════
        // char 参与运算
        // ═══════════════════════════════════════════════════════════════
        
        char letter = 'A';
        System.out.println(letter + 1);        // 66 (int)
        System.out.println((char)(letter + 1)); // B
        
        // 大小写转换
        char lower = 'a';
        char upper = (char)(lower - 32);  // 'A'
        // 或使用 Character 类
        upper = Character.toUpperCase(lower);
        
        // ═══════════════════════════════════════════════════════════════
        // 字符判断
        // ═══════════════════════════════════════════════════════════════
        
        char ch = '9';
        System.out.println(Character.isDigit(ch));       // true
        System.out.println(Character.isLetter(ch));      // false
        System.out.println(Character.isLetterOrDigit(ch)); // true
        System.out.println(Character.isWhitespace(' ')); // true
    }
}

4.2 char 与 String 的区别

// ═══════════════════════════════════════════════════════════════════════
// char 与 String 的区别
// ═══════════════════════════════════════════════════════════════════════

char c = 'A';          // 单引号,基本类型,2 字节
String s = "A";        // 双引号,引用类型,对象

// 内存占用不同
char[] chars = {'H', 'e', 'l', 'l', 'o'};  // 10 字节
String str = "Hello";  // 对象头 + 引用 + char[] + ...(更多)

// 运算不同
System.out.println('A' + 'B');    // 131 (int 相加)
System.out.println("A" + "B");    // "AB" (字符串拼接)
System.out.println('A' + "B");    // "AB" (char 转 String 后拼接)

// 空值不同
char c1 = '\u0000';   // char 的"空"值
String s1 = null;     // String 可以为 null
String s2 = "";       // String 可以为空串

4.3 常用转义字符

<mxfile>
  <diagram id="escape-characters" name="常用转义字符">
    <mxGraphModel dx="900" dy="700" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700" 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=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="80" y="30" width="740" height="620" as="geometry" />
        </mxCell>
        
        <!-- 标题 -->
        <mxCell id="3" value="常用转义字符" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=26;fontStyle=1;fontColor=#1a237e;" vertex="1" parent="1">
          <mxGeometry x="250" y="50" width="400" height="50" as="geometry" />
        </mxCell>
        
        <!-- 表头背景 -->
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3949ab;strokeColor=none;arcSize=5;" vertex="1" parent="1">
          <mxGeometry x="120" y="115" width="660" height="55" as="geometry" />
        </mxCell>
        
        <!-- 表头 - 转义字符 -->
        <mxCell id="5" value="转义字符" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="120" y="115" width="180" height="55" as="geometry" />
        </mxCell>
        
        <!-- 表头 - 说明 -->
        <mxCell id="6" value="说明" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="300" y="115" width="480" height="55" as="geometry" />
        </mxCell>
        
        <!-- 垂直分隔线 -->
        <mxCell id="7" value="" style="endArrow=none;html=1;strokeColor=#e0e0e0;strokeWidth=2;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="300" y="170" as="sourcePoint" />
            <mxPoint x="300" y="610" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        
        <!-- 行1: \n -->
        <mxCell id="8" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="120" y="170" width="660" height="48" as="geometry" />
        </mxCell>
        <mxCell id="9" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1565c0;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="145" y="179" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="10" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;\n&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="145" y="179" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="11" value="换行符 (LF)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="320" y="170" width="440" height="48" as="geometry" />
        </mxCell>
        
        <!-- 行2: \r -->
        <mxCell id="12" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="120" y="218" width="660" height="48" as="geometry" />
        </mxCell>
        <mxCell id="13" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#2e7d32;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="145" y="227" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="14" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;\r&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="145" y="227" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="15" value="回车符 (CR)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="320" y="218" width="440" height="48" as="geometry" />
        </mxCell>
        
        <!-- 行3: \t -->
        <mxCell id="16" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="120" y="266" width="660" height="48" as="geometry" />
        </mxCell>
        <mxCell id="17" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#6a1b9a;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="145" y="275" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="18" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;\t&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="145" y="275" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="19" value="制表符 (Tab)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="320" y="266" width="440" height="48" as="geometry" />
        </mxCell>
        
        <!-- 行4: \\ -->
        <mxCell id="20" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="120" y="314" width="660" height="48" as="geometry" />
        </mxCell>
        <mxCell id="21" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c62828;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="145" y="323" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="22" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;\\&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="145" y="323" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="23" value="反斜杠" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="320" y="314" width="440" height="48" as="geometry" />
        </mxCell>
        
        <!-- 行5: \' -->
        <mxCell id="24" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="120" y="362" width="660" height="48" as="geometry" />
        </mxCell>
        <mxCell id="25" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ef6c00;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="145" y="371" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="26" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;\'&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="145" y="371" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="27" value="单引号" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="320" y="362" width="440" height="48" as="geometry" />
        </mxCell>
        
        <!-- 行6: \" -->
        <mxCell id="28" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="120" y="410" width="660" height="48" as="geometry" />
        </mxCell>
        <mxCell id="29" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#00838f;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="145" y="419" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="30" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;\&quot;&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="145" y="419" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="31" value="双引号" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="320" y="410" width="440" height="48" as="geometry" />
        </mxCell>
        
        <!-- 行7: \b -->
        <mxCell id="32" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="120" y="458" width="660" height="48" as="geometry" />
        </mxCell>
        <mxCell id="33" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#5d4037;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="145" y="467" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="34" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;\b&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="145" y="467" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="35" value="退格符" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="320" y="458" width="440" height="48" as="geometry" />
        </mxCell>
        
        <!-- 行8: \f -->
        <mxCell id="36" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="120" y="506" width="660" height="48" as="geometry" />
        </mxCell>
        <mxCell id="37" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#455a64;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="145" y="515" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="38" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;\f&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="145" y="515" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="39" value="换页符" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="320" y="506" width="440" height="48" as="geometry" />
        </mxCell>
        
        <!-- 行9: \uXXXX -->
        <mxCell id="40" value="" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="120" y="554" width="660" height="56" as="geometry" />
        </mxCell>
        <mxCell id="41" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d84315;strokeColor=none;" vertex="1" parent="1">
          <mxGeometry x="145" y="567" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="42" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;\uXXXX&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="145" y="567" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="43" value="Unicode 字符(4 位十六进制)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;" vertex="1" parent="1">
          <mxGeometry x="320" y="554" width="350" height="56" as="geometry" />
        </mxCell>
        
        <!-- Unicode 示例标签 -->
        <mxCell id="44" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffccbc;strokeColor=#ff7043;" vertex="1" parent="1">
          <mxGeometry x="620" y="567" width="140" height="30" as="geometry" />
        </mxCell>
        <mxCell id="45" value="例: &lt;font face=&quot;Courier New&quot;&gt;\u0041&lt;/font&gt; = A" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#d84315;" vertex="1" parent="1">
          <mxGeometry x="620" y="567" width="140" height="30" as="geometry" />
        </mxCell>
        </root>
    </mxGraphModel>
  </diagram>
</mxfile>

五、布尔类型

public class BooleanDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // boolean 只有两个值
        // ═══════════════════════════════════════════════════════════════

        boolean flag = true;
        boolean isEmpty = false;

        // ═══════════════════════════════════════════════════════════════
        // boolean 不能与其他类型转换
        // ═══════════════════════════════════════════════════════════════

        // int num = true;       // ❌ 编译错误
        // boolean b = 1;        // ❌ 编译错误
        // if (1) { }            // ❌ 编译错误(不像 C/C++)

        // 正确写法
        int num = 1;
        if (num != 0) { }       // ✅ 显式比较
        if (flag) { }           // ✅ 直接使用 boolean

        // ═══════════════════════════════════════════════════════════════
        // boolean 的大小
        // ═══════════════════════════════════════════════════════════════

        // JVM 规范未明确定义 boolean 的大小
        // 实际实现中:
        // - 单个 boolean 通常占 4 字节(作为 int 处理)
        // - boolean 数组中每个元素占 1 字节
    }
}
<mxfile>
  <diagram id="boolean-jvm" name="boolean的JVM实现">
    <mxGraphModel dx="900" dy="700" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" value="boolean 的 JVM 实现" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#1E293B;" vertex="1" parent="1">
          <mxGeometry x="250" y="20" width="400" height="40" as="geometry" />
        </mxCell>
        <mxCell id="3" value="深入理解 JVM 规范中的 boolean 处理机制" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="275" y="58" width="350" height="25" as="geometry" />
        </mxCell>
        <mxCell id="4" value="⚠️ 核心事实" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="40" y="100" width="110" height="30" as="geometry" />
        </mxCell>
        <mxCell id="5" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#EF4444;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="40" y="140" width="820" height="50" as="geometry" />
        </mxCell>
        <mxCell id="6" value="JVM 规范中,boolean 没有专门的字节码指令!" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#991B1B;" vertex="1" parent="1">
          <mxGeometry x="200" y="150" width="500" height="30" as="geometry" />
        </mxCell>
        <mxCell id="7" value="单个 boolean 变量" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="40" y="210" width="160" height="30" as="geometry" />
        </mxCell>
        <mxCell id="8" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#3B82F6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="40" y="250" width="400" height="170" as="geometry" />
        </mxCell>
        <mxCell id="9" value="使用 int 的指令处理" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#1E40AF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="55" y="265" width="160" height="28" as="geometry" />
        </mxCell>
        <mxCell id="10" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BFDBFE;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="55" y="305" width="160" height="100" as="geometry" />
        </mxCell>
        <mxCell id="11" value="值映射" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="315" width="60" height="20" as="geometry" />
        </mxCell>
        <mxCell id="12" value="true" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="70" y="345" width="55" height="28" as="geometry" />
        </mxCell>
        <mxCell id="13" value="→" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#64748B;" vertex="1" parent="1">
          <mxGeometry x="125" y="345" width="30" height="28" as="geometry" />
        </mxCell>
        <mxCell id="14" value="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#22C55E;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="155" y="345" width="45" height="28" as="geometry" />
        </mxCell>
        <mxCell id="15" value="false" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="70" y="378" width="55" height="28" as="geometry" />
        </mxCell>
        <mxCell id="16" value="→" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#64748B;" vertex="1" parent="1">
          <mxGeometry x="125" y="378" width="30" height="28" as="geometry" />
        </mxCell>
        <mxCell id="17" value="0" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#EF4444;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="155" y="378" width="45" height="28" as="geometry" />
        </mxCell>
        <mxCell id="18" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BFDBFE;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="230" y="305" width="195" height="100" as="geometry" />
        </mxCell>
        <mxCell id="19" value="实际内存占用" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="240" y="315" width="90" height="20" as="geometry" />
        </mxCell>
        <mxCell id="20" value="4 字节" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#92400E;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="255" y="350" width="100" height="40" as="geometry" />
        </mxCell>
        <mxCell id="21" value="= 32 位" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontColor=#78350F;" vertex="1" parent="1">
          <mxGeometry x="355" y="355" width="55" height="30" as="geometry" />
        </mxCell>
        <mxCell id="22" value="(与 int 相同)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#92400E;" vertex="1" parent="1">
          <mxGeometry x="265" y="390" width="80" height="18" as="geometry" />
        </mxCell>
        <mxCell id="23" value="boolean 数组" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="460" y="210" width="120" height="30" as="geometry" />
        </mxCell>
        <mxCell id="24" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="460" y="250" width="400" height="170" as="geometry" />
        </mxCell>
        <mxCell id="25" value="使用 byte 数组实现" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#166534;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="475" y="265" width="150" height="28" as="geometry" />
        </mxCell>
        <mxCell id="26" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="475" y="305" width="370" height="100" as="geometry" />
        </mxCell>
        <mxCell id="27" value="每个元素占用" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="315" width="90" height="20" as="geometry" />
        </mxCell>
        <mxCell id="28" value="1 字节" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=20;fontStyle=1;fontColor=#166534;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="495" y="345" width="80" height="35" as="geometry" />
        </mxCell>
        <mxCell id="29" value="= 8 位" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontColor=#166534;" vertex="1" parent="1">
          <mxGeometry x="575" y="350" width="45" height="25" as="geometry" />
        </mxCell>
        <mxCell id="30" value="boolean[] 内存布局示意:" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#166534;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="630" y="315" width="150" height="20" as="geometry" />
        </mxCell>
        <mxCell id="31" value="[0]" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=10;fontStyle=1;fontColor=#FFFFFF;" vertex="1" parent="1">
          <mxGeometry x="640" y="340" width="35" height="30" as="geometry" />
        </mxCell>
        <mxCell id="32" value="[1]" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontSize=10;fontStyle=1;fontColor=#FFFFFF;" vertex="1" parent="1">
          <mxGeometry x="675" y="340" width="35" height="30" as="geometry" />
        </mxCell>
        <mxCell id="33" value="[2]" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=10;fontStyle=1;fontColor=#FFFFFF;" vertex="1" parent="1">
          <mxGeometry x="710" y="340" width="35" height="30" as="geometry" />
        </mxCell>
        <mxCell id="34" value="[3]" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontSize=10;fontStyle=1;fontColor=#FFFFFF;" vertex="1" parent="1">
          <mxGeometry x="745" y="340" width="35" height="30" as="geometry" />
        </mxCell>
        <mxCell id="35" value="..." style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#64748B;" vertex="1" parent="1">
          <mxGeometry x="785" y="340" width="30" height="30" as="geometry" />
        </mxCell>
        <mxCell id="36" value="1B" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=9;fontColor=#64748B;" vertex="1" parent="1">
          <mxGeometry x="640" y="370" width="35" height="15" as="geometry" />
        </mxCell>
        <mxCell id="37" value="1B" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=9;fontColor=#64748B;" vertex="1" parent="1">
          <mxGeometry x="675" y="370" width="35" height="15" as="geometry" />
        </mxCell>
        <mxCell id="38" value="1B" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=9;fontColor=#64748B;" vertex="1" parent="1">
          <mxGeometry x="710" y="370" width="35" height="15" as="geometry" />
        </mxCell>
        <mxCell id="39" value="1B" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=9;fontColor=#64748B;" vertex="1" parent="1">
          <mxGeometry x="745" y="370" width="35" height="15" as="geometry" />
        </mxCell>
        <mxCell id="40" value="📝 字节码示例" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="40" y="440" width="120" height="30" as="geometry" />
        </mxCell>
        <mxCell id="41" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#8B5CF6;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="40" y="480" width="820" height="180" as="geometry" />
        </mxCell>
        <mxCell id="42" value="Java 源代码" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="55" y="495" width="100" height="24" as="geometry" />
        </mxCell>
        <mxCell id="43" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="55" y="530" width="250" height="50" as="geometry" />
        </mxCell>
        <mxCell id="44" value="&lt;font face=&quot;Courier New&quot; color=&quot;#f472b6&quot;&gt;boolean&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#e2e8f0&quot;&gt; flag = &lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#4ade80&quot;&gt;true&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#e2e8f0&quot;&gt;;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="60" y="540" width="240" height="30" as="geometry" />
        </mxCell>
        <mxCell id="45" value="" style="shape=flexArrow;endArrow=classic;html=1;fillColor=#8B5CF6;strokeColor=none;width=20;endSize=6;endWidth=15;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="320" y="555" as="sourcePoint" />
            <mxPoint x="380" y="555" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="46" value="编译" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#7C3AED;" vertex="1" parent="1">
          <mxGeometry x="330" y="535" width="40" height="18" as="geometry" />
        </mxCell>
        <mxCell id="47" value="JVM 字节码" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="395" y="495" width="100" height="24" as="geometry" />
        </mxCell>
        <mxCell id="48" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="395" y="530" width="200" height="115" as="geometry" />
        </mxCell>
        <mxCell id="49" value="&lt;font face=&quot;Courier New&quot; color=&quot;#fbbf24&quot;&gt;iconst_1&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="400" y="540" width="100" height="25" as="geometry" />
        </mxCell>
        <mxCell id="50" value="&lt;font face=&quot;Courier New&quot; color=&quot;#fbbf24&quot;&gt;istore_1&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="400" y="570" width="100" height="25" as="geometry" />
        </mxCell>
        <mxCell id="51" value="&lt;font color=&quot;#94a3b8&quot;&gt;// 将常量 1 压栈&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="400" y="600" width="180" height="20" as="geometry" />
        </mxCell>
        <mxCell id="52" value="&lt;font color=&quot;#94a3b8&quot;&gt;// 存储到局部变量表&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="400" y="620" width="180" height="20" as="geometry" />
        </mxCell>
        <mxCell id="53" value="关键说明" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="620" y="495" width="80" height="24" as="geometry" />
        </mxCell>
        <mxCell id="54" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="620" y="530" width="225" height="115" as="geometry" />
        </mxCell>
        <mxCell id="55" value="iconst_1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#991B1B;fontFamily=Courier New;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="635" y="545" width="70" height="22" as="geometry" />
        </mxCell>
        <mxCell id="56" value="是 int 类型的指令" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#991B1B;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="710" y="545" width="120" height="22" as="geometry" />
        </mxCell>
        <mxCell id="57" value="istore_1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#991B1B;fontFamily=Courier New;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="635" y="575" width="70" height="22" as="geometry" />
        </mxCell>
        <mxCell id="58" value="是 int 类型的指令" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#991B1B;spacingLeft=5;" vertex="1" parent="1">
          <mxGeometry x="710" y="575" width="120" height="22" as="geometry" />
        </mxCell>
        <mxCell id="59" value="💡 没有 bconst、bstore 指令!" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="635" y="610" width="195" height="25" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

六、类型转换

6.1 自动类型转换(隐式转换)

 <mxfile>
  <diagram id="auto-type-conversion" name="自动类型转换规则">
    <mxGraphModel dx="900" dy="680" 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" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="30" width="780" height="520" as="geometry" />
        </mxCell>
        
        <!-- 标题 -->
        <mxCell id="3" value="自动类型转换规则" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=26;fontStyle=1;fontColor=#1a237e;" vertex="1" parent="1">
          <mxGeometry x="250" y="50" width="400" height="50" as="geometry" />
        </mxCell>
        
        <!-- 规则说明 -->
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="100" y="115" width="700" height="50" as="geometry" />
        </mxCell>
        <mxCell id="5" value="小类型 → 大类型:自动转换,无精度损失" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontColor=#2e7d32;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="115" width="700" height="50" as="geometry" />
        </mxCell>
        
        <!-- 转换流程图区域 -->
        <mxCell id="6" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;dashed=1;" vertex="1" parent="1">
          <mxGeometry x="100" y="185" width="700" height="200" as="geometry" />
        </mxCell>
        
        <!-- byte -->
        <mxCell id="7" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1565c0;strokeColor=none;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="120" y="230" width="80" height="45" as="geometry" />
        </mxCell>
        <mxCell id="8" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;byte&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="120" y="230" width="80" height="45" as="geometry" />
        </mxCell>
        <!-- byte → short 箭头 -->
        <mxCell id="9" value="" style="endArrow=classic;html=1;strokeColor=#1976d2;strokeWidth=3;entryX=0;entryY=0.5;exitX=1;exitY=0.5;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="200" y="252" as="sourcePoint" />
            <mxPoint x="230" y="252" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        
        <!-- short -->
        <mxCell id="10" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="230" y="230" width="80" height="45" as="geometry" />
        </mxCell>
        <mxCell id="11" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;short&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="230" y="230" width="80" height="45" as="geometry" />
        </mxCell>
        
        <!-- short → int 箭头 -->
        <mxCell id="12" value="" style="endArrow=classic;html=1;strokeColor=#1976d2;strokeWidth=3;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="310" y="252" as="sourcePoint" />
            <mxPoint x="340" y="252" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        
        <!-- int -->
        <mxCell id="13" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#2196f3;strokeColor=none;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="340" y="230" width="80" height="45" as="geometry" />
        </mxCell>
        <mxCell id="14" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="340" y="230" width="80" height="45" as="geometry" />
        </mxCell>
        
        <!-- int → long 箭头 -->
        <mxCell id="15" value="" style="endArrow=classic;html=1;strokeColor=#1976d2;strokeWidth=3;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="420" y="252" as="sourcePoint" />
            <mxPoint x="450" y="252" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        
        <!-- long -->
        <mxCell id="16" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#42a5f5;strokeColor=none;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="450" y="230" width="80" height="45" as="geometry" />
        </mxCell>
        <mxCell id="17" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;long&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="450" y="230" width="80" height="45" as="geometry" />
        </mxCell>
        
        <!-- long → float 箭头 -->
        <mxCell id="18" value="" style="endArrow=classic;html=1;strokeColor=#ff9800;strokeWidth=3;dashed=1;dashPattern=8 4;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="530" y="252" as="sourcePoint" />
            <mxPoint x="570" y="252" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        
        <!-- float -->
        <mxCell id="19" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7cb342;strokeColor=none;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="570" y="230" width="80" height="45" as="geometry" />
        </mxCell>
        <mxCell id="20" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;float&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="570" y="230" width="80" height="45" as="geometry" />
        </mxCell>
        
        <!-- float → double 箭头 -->
        <mxCell id="21" value="" style="endArrow=classic;html=1;strokeColor=#4caf50;strokeWidth=3;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="650" y="252" as="sourcePoint" />
            <mxPoint x="690" y="252" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        
        <!-- double -->
        <mxCell id="22" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#2e7d32;strokeColor=none;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="690" y="230" width="90" height="45" as="geometry" />
        </mxCell>
        <mxCell id="23" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;double&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="690" y="230" width="90" height="45" as="geometry" />
        </mxCell>
        
        <!-- char -->
        <mxCell id="24" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7b1fa2;strokeColor=none;shadow=1;" vertex="1" parent="1">
          <mxGeometry x="120" y="320" width="80" height="45" as="geometry" />
        </mxCell>
        <mxCell id="25" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;char&lt;/b&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#ffffff;" vertex="1" parent="1">
          <mxGeometry x="120" y="320" width="80" height="45" as="geometry" />
        </mxCell>
        
        <!-- char → int 箭头 (曲线) -->
        <mxCell id="26" value="" style="endArrow=classic;html=1;strokeColor=#7b1fa2;strokeWidth=3;curved=0;exitX=1;exitY=0.5;entryX=0.5;entryY=1;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="200" y="342" as="sourcePoint" />
            <mxPoint x="380" y="275" as="targetPoint" /><Array as="points">
              <mxPoint x="380" y="342" />
            </Array>
          </mxGeometry>
        </mxCell>
        
        <!-- char 到 byte 的向上箭头 -->
        <mxCell id="27" value="" style="endArrow=none;html=1;strokeColor=#9e9e9e;strokeWidth=2;dashed=1;startArrow=classic;startFill=1;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="160" y="320" as="sourcePoint" />
            <mxPoint x="160" y="275" as="targetPoint" /></mxGeometry>
        </mxCell>
        
        <!-- 图例区域 -->
        <mxCell id="28" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff8e1;strokeColor=#ffa000;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="100" y="405" width="700" height="125" as="geometry" />
        </mxCell>
        
        <!-- 警告图标 -->
        <mxCell id="29" value="⚠️" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;" vertex="1" parent="1">
          <mxGeometry x="115" y="415" width="50" height="50" as="geometry" />
        </mxCell>
        
        <!-- 注意标题 -->
        <mxCell id="30" value="注意:精度损失风险" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;fontStyle=1;fontColor=#e65100;" vertex="1" parent="1">
          <mxGeometry x="165" y="420" width="200" height="35" as="geometry" />
        </mxCell>
        
        <!-- 图例 - 安全转换 -->
        <mxCell id="31" value="" style="endArrow=classic;html=1;strokeColor=#1976d2;strokeWidth=3;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="420" y="437" as="sourcePoint" />
            <mxPoint x="480" y="437" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="32" value="安全转换" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#1976d2;" vertex="1" parent="1">
          <mxGeometry x="490" y="422" width="80" height="30" as="geometry" />
        </mxCell>
        
        <!-- 图例 - 可能损失精度 -->
        <mxCell id="33" value="" style="endArrow=classic;html=1;strokeColor=#ff9800;strokeWidth=3;dashed=1;dashPattern=8 4;" edge="1" parent="1">
          <mxGeometry width="50" height="50" relative="1" as="geometry">
            <mxPoint x="590" y="437" as="sourcePoint" />
            <mxPoint x="650" y="437" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="34" value="可能损失精度" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#ff9800;" vertex="1" parent="1">
          <mxGeometry x="660" y="422" width="120" height="30" as="geometry" />
        </mxCell>
        
        <!-- 详细说明1 -->
        <mxCell id="35" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffccbc;strokeColor=#ff7043;" vertex="1" parent="1">
          <mxGeometry x="130" y="460" width="300" height="55" as="geometry" />
        </mxCell>
        <mxCell id="36" value="&lt;font face=&quot;Courier New&quot; color=&quot;#d84315&quot;&gt;int → float&lt;/font&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#d84315&quot;&gt;long → float/double&lt;/font&gt;&lt;br&gt;可能有精度损失" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" vertex="1" parent="1">
          <mxGeometry x="130" y="460" width="300" height="55" as="geometry" />
        </mxCell>
        
        <!-- 详细说明2 -->
        <mxCell id="37" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1bee7;strokeColor=#9c27b0;" vertex="1" parent="1">
          <mxGeometry x="460" y="460" width="320" height="55" as="geometry" />
        </mxCell>
        <mxCell id="38" value="原因:浮点数的&lt;b&gt;尾数位有限&lt;/b&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot;&gt;float&lt;/font&gt; 尾数 23 位,&lt;font face=&quot;Courier New&quot;&gt;double&lt;/font&gt; 尾数 52 位" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#6a1b9a;" vertex="1" parent="1">
          <mxGeometry x="460" y="460" width="320" height="55" as="geometry" />
        </mxCell>
        
        <!-- 类型大小标注 -->
        <mxCell id="39" value="1字节" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=10;fontColor=#757575;" vertex="1" parent="1">
          <mxGeometry x="120" y="200" width="80" height="25" as="geometry" />
        </mxCell>
        <mxCell id="40" value="2字节" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=10;fontColor=#757575;" vertex="1" parent="1">
          <mxGeometry x="230" y="200" width="80" height="25" as="geometry" />
        </mxCell>
        <mxCell id="41" value="4字节" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=10;fontColor=#757575;" vertex="1" parent="1">
          <mxGeometry x="340" y="200" width="80" height="25" as="geometry" />
        </mxCell>
        <mxCell id="42" value="8字节" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=10;fontColor=#757575;" vertex="1" parent="1">
          <mxGeometry x="450" y="200" width="80" height="25" as="geometry" />
        </mxCell>
        <mxCell id="43" value="4字节" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=10;fontColor=#757575;" vertex="1" parent="1">
          <mxGeometry x="570" y="200" width="80" height="25" as="geometry" />
        </mxCell>
        <mxCell id="44" value="8字节" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=10;fontColor=#757575;" vertex="1" parent="1">
          <mxGeometry x="690" y="200" width="90" height="25" as="geometry" />
        </mxCell>
        <mxCell id="45" value="2字节" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=10;fontColor=#757575;" vertex="1" parent="1">
          <mxGeometry x="120" y="365" width="80" height="20" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
public class AutoConversionDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // 自动类型转换
        // ═══════════════════════════════════════════════════════════════

        byte b = 10;
        int i = b;      // byte → int,自动转换
        long l = i;     // int → long,自动转换
        float f = l;    // long → float,自动转换
        double d = f;   // float → double,自动转换

        char c = 'A';
        int ic = c;     // char → int,自动转换 (65)

        // ═══════════════════════════════════════════════════════════════
        // 精度损失示例
        // ═══════════════════════════════════════════════════════════════

        int bigInt = 123456789;
        float floatVal = bigInt;  // 自动转换,但有精度损失
        System.out.println("int: " + bigInt);        // 123456789
        System.out.println("float: " + floatVal);    // 1.23456792E8
        System.out.println("转回int: " + (int)floatVal);  // 123456792

        long bigLong = 123456789012345678L;
        double doubleVal = bigLong;  // 精度损失
        System.out.println("long: " + bigLong);      // 123456789012345678
        System.out.println("double: " + doubleVal);  // 1.2345678901234568E17
    }
}

6.2 强制类型转换(显式转换)

public class CastDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // 大类型 → 小类型:需要强制转换
        // ═══════════════════════════════════════════════════════════════

        int i = 128;
        byte b = (byte) i;  // 必须强转
        System.out.println("int 128 → byte = " + b);  // -128 (溢出)

        double d = 3.99;
        int truncated = (int) d;  // 截断小数部分
        System.out.println("double 3.99 → int = " + truncated);  // 3

        // ═══════════════════════════════════════════════════════════════
        // 强转可能导致数据丢失
        // ═══════════════════════════════════════════════════════════════

        int big = 300;
        byte small = (byte) big;
        System.out.println("int 300 → byte = " + small);  // 44

        // 原因分析:
        // 300 的二进制: 00000000 00000000 00000001 00101100
        // 截取低 8 位:                            00101100 = 44
    }
}
<mxfile>
  <diagram id="type-cast-truncation" name="强制转换数据截断图解">
    <mxGraphModel dx="1068" dy="484" 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="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#1E293B;" value="强制转换数据截断图解" vertex="1">
          <mxGeometry height="40" width="450" x="225" y="15" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;" value="理解大类型转小类型时的数据丢失" vertex="1">
          <mxGeometry height="25" width="300" x="300" y="52" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="示例 1:int 300 → byte" vertex="1">
          <mxGeometry height="30" width="180" x="40" y="95" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="250" width="820" x="40" y="135" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E40AF;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="int (32位)" vertex="1">
          <mxGeometry height="26" width="90" x="60" y="155" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#E5E7EB;strokeColor=#9CA3AF;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#6B7280;fontFamily=Courier New;" value="00000000" vertex="1">
          <mxGeometry height="36" width="100" x="170" y="150" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#E5E7EB;strokeColor=#9CA3AF;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#6B7280;fontFamily=Courier New;" value="00000000" vertex="1">
          <mxGeometry height="36" width="100" x="270" y="150" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#991B1B;fontFamily=Courier New;" value="00000001" vertex="1">
          <mxGeometry height="36" width="100" x="370" y="150" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#22C55E;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#166534;fontFamily=Courier New;" value="00101100" vertex="1">
          <mxGeometry height="36" width="100" x="470" y="150" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="= 300" vertex="1">
          <mxGeometry height="32" width="70" x="590" y="152" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#991B1B;rounded=1;" value="被截断" vertex="1">
          <mxGeometry height="18" width="50" x="395" y="190" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#166534;rounded=1;" value="保留" vertex="1">
          <mxGeometry height="18" width="50" x="495" y="188" as="geometry" />
        </mxCell>
        <mxCell id="14" edge="1" parent="1" style="shape=flexArrow;endArrow=classic;html=1;fillColor=#EF4444;strokeColor=none;width=30;endSize=8;endWidth=25;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="520" y="220" as="sourcePoint" />
            <mxPoint x="520" y="270" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="15" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#EF4444;" value="强制转换" vertex="1">
          <mxGeometry height="20" width="70" x="540" y="235" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=#166534;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="byte (8位)" vertex="1">
          <mxGeometry height="26" width="90" x="60" y="285" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=3;fontSize=16;fontStyle=1;fontColor=#FFFFFF;fontFamily=Courier New;" value="00101100" vertex="1">
          <mxGeometry height="40" width="100" x="470" y="280" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="= 44" vertex="1">
          <mxGeometry height="32" width="60" x="590" y="284" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#92400E;rounded=1;" value="⚠️ 高位被截断,数值从 300 变成 44" vertex="1">
          <mxGeometry height="28" width="280" x="380" y="335" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BFDBFE;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="130" width="165" x="680" y="150" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💡 计算过程" vertex="1">
          <mxGeometry height="22" width="85" x="690" y="160" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#1E40AF;spacingLeft=5;" value="300 = 256 + 44" vertex="1">
          <mxGeometry height="20" width="140" x="690" y="190" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#1E40AF;spacingLeft=5;" value="300 % 256 = 44" vertex="1">
          <mxGeometry height="20" width="140" x="690" y="215" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=10;fontColor=#1E40AF;rounded=1;" value="byte范围: -128~127" vertex="1">
          <mxGeometry height="20" width="120" x="695" y="245" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="示例 2:int 128 → byte(符号位变化)" vertex="1">
          <mxGeometry height="30" width="280" x="40" y="400" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#8B5CF6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="280" width="820" x="40" y="440" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#6D28D9;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="int (32位)" vertex="1">
          <mxGeometry height="26" width="90" x="60" y="460" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#E5E7EB;strokeColor=#9CA3AF;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#6B7280;fontFamily=Courier New;" value="00000000" vertex="1">
          <mxGeometry height="36" width="100" x="170" y="455" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#E5E7EB;strokeColor=#9CA3AF;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#6B7280;fontFamily=Courier New;" value="00000000" vertex="1">
          <mxGeometry height="36" width="100" x="270" y="455" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#E5E7EB;strokeColor=#9CA3AF;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#6B7280;fontFamily=Courier New;" value="00000000" vertex="1">
          <mxGeometry height="36" width="100" x="370" y="455" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#8B5CF6;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#6D28D9;fontFamily=Courier New;" value="10000000" vertex="1">
          <mxGeometry height="36" width="100" x="470" y="455" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="= 128" vertex="1">
          <mxGeometry height="32" width="70" x="590" y="457" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="正数" vertex="1">
          <mxGeometry height="22" width="40" x="662.5" y="464" as="geometry" />
        </mxCell>
        <mxCell id="34" edge="1" parent="1" style="shape=flexArrow;endArrow=classic;html=1;fillColor=#8B5CF6;strokeColor=none;width=30;endSize=8;endWidth=25;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="520" y="510" as="sourcePoint" />
            <mxPoint x="520" y="560" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#8B5CF6;" value="强制转换" vertex="1">
          <mxGeometry height="20" width="70" x="540" y="525" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=#DC2626;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="byte (8位)" vertex="1">
          <mxGeometry height="26" width="90" x="60" y="575" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=3;fontSize=16;fontStyle=1;fontColor=#FFFFFF;fontFamily=Courier New;" value="" vertex="1">
          <mxGeometry height="40" width="100" x="470" y="570" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#EF4444;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#DC2626;fontFamily=Courier New;" value="1" vertex="1">
          <mxGeometry height="40" width="16" x="470" y="570" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=16;fontStyle=1;fontColor=#FFFFFF;fontFamily=Courier New;" value="0000000" vertex="1">
          <mxGeometry height="24" width="84" x="486" y="578" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="= -128" vertex="1">
          <mxGeometry height="32" width="70" x="590" y="574" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="负数!" vertex="1">
          <mxGeometry height="22" width="45" x="662.5" y="581" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=9;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="符号位" vertex="1">
          <mxGeometry height="16" width="45" x="455.5" y="619" as="geometry" />
        </mxCell>
        <mxCell id="43" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#EF4444;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="477.76" y="622" as="sourcePoint" />
            <mxPoint x="477.76" y="606" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#E9D5FF;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="134" width="140" x="710" y="490" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="⚡ 关键变化" vertex="1">
          <mxGeometry height="22" width="85" x="720" y="496" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#6D28D9;spacingLeft=5;" value="int 中: 最高位是数据位" vertex="1">
          <mxGeometry height="18" width="145" x="720" y="526" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#DC2626;spacingLeft=5;" value="byte中: 最高位是符号位" vertex="1">
          <mxGeometry height="18" width="145" x="720" y="546" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#991B1B;rounded=1;" value="1 → 表示负数" vertex="1">
          <mxGeometry height="20" width="90" x="730" y="571" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="128 变成 -128" vertex="1">
          <mxGeometry height="22" width="100" x="725" y="596" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#991B1B;rounded=1;" value="⚠️ 最高位从数据位变成符号位,正数变负数!" vertex="1">
          <mxGeometry height="28" width="340" x="330" y="635" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📌 总结:强制转换可能导致数据截断和符号变化,使用时需谨慎!" vertex="1">
          <mxGeometry height="35" width="550" x="175" y="680" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

6.3 表达式中的类型提升

public class TypePromotionDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // 规则 1:byte、short、char 运算时自动提升为 int
        // ═══════════════════════════════════════════════════════════════
        
        byte b1 = 10;
        byte b2 = 20;
        // byte b3 = b1 + b2;  // ❌ 编译错误,结果是 int
        int i = b1 + b2;       // ✅ 正确
        byte b3 = (byte)(b1 + b2);  // ✅ 强转
        
        char c1 = 'A';
        char c2 = 'B';
        // char c3 = c1 + c2;  // ❌ 编译错误
        int ic = c1 + c2;      // ✅ 正确,结果是 131
        
        // ═══════════════════════════════════════════════════════════════
        // 规则 2:表达式结果类型是最大的操作数类型
        // ═══════════════════════════════════════════════════════════════
        
        int a = 10;
        long b = 20L;
        float c = 1.5f;
        double d = 2.5;
        
        // int + long = long
        long r1 = a + b;
        
        // long + float = float  
        float r2 = b + c;
        
        // float + double = double
        double r3 = c + d;
        
        // 混合运算:结果是 double
        double r4 = a + b + c + d;
        
        // ═══════════════════════════════════════════════════════════════
        // 特殊情况:字面量
        // ═══════════════════════════════════════════════════════════════
        
        byte b4 = 10 + 20;  // ✅ 编译期常量折叠,可以直接赋值
        // byte b5 = b1 + 20;  // ❌ 变量参与运算,结果是 int
        
        final byte fb1 = 10;
        final byte fb2 = 20;
        byte b6 = fb1 + fb2;  // ✅ final 常量,编译期可确定
    }
}

七、包装类

7.1 基本类型与包装类对应关系

<mxfile>
  <diagram id="primitive-wrapper-types-fixed" name="基本类型与包装类对应关系">
    <mxGraphModel dx="1281" dy="654" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="900" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#1E293B;" value="Java 基本类型与包装类对应关系" vertex="1">
          <mxGeometry height="40" width="450" x="225" y="15" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;" value="Java 8种基本类型及其包装类的映射与继承体系" vertex="1">
          <mxGeometry height="25" width="400" x="250" y="52" as="geometry" />
        </mxCell>

        <mxCell id="4" 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="28" width="120" x="40" y="95" as="geometry" />
        </mxCell>

        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#E2E8F0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="307" width="820" x="40" y="133" as="geometry" />
        </mxCell>

        <mxCell id="6" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="基本类型" vertex="1">
          <mxGeometry height="35" width="120" x="60" y="150" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="包装类" vertex="1">
          <mxGeometry height="35" width="140" x="180" y="150" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="继承关系" vertex="1">
          <mxGeometry height="35" width="200" x="320" y="150" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#FFFFFF;" value="分类" vertex="1">
          <mxGeometry height="35" width="120" x="520" y="150" as="geometry" />
        </mxCell>

        <mxCell id="10" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#BFDBFE;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="byte" vertex="1">
          <mxGeometry height="30" width="120" x="60" y="185" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="Byte" vertex="1">
          <mxGeometry height="30" width="140" x="180" y="185" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=11;fontColor=#1E40AF;" value="Number → Object" vertex="1">
          <mxGeometry height="30" width="200" x="320" y="185" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="数值型" vertex="1">
          <mxGeometry height="30" width="120" x="520" y="185" as="geometry" />
        </mxCell>

        <mxCell id="14" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#BFDBFE;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="short" vertex="1">
          <mxGeometry height="30" width="120" x="60" y="215" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="Short" vertex="1">
          <mxGeometry height="30" width="140" x="180" y="215" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=11;fontColor=#1E40AF;" value="Number → Object" vertex="1">
          <mxGeometry height="30" width="200" x="320" y="215" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="数值型" vertex="1">
          <mxGeometry height="30" width="120" x="520" y="215" as="geometry" />
        </mxCell>

        <mxCell id="18" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#BFDBFE;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="int" vertex="1">
          <mxGeometry height="30" width="120" x="60" y="245" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="Integer" vertex="1">
          <mxGeometry height="30" width="140" x="180" y="245" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=11;fontColor=#1E40AF;" value="Number → Object" vertex="1">
          <mxGeometry height="30" width="200" x="320" y="245" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="数值型" vertex="1">
          <mxGeometry height="30" width="120" x="520" y="245" as="geometry" />
        </mxCell>

        <mxCell id="22" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#BFDBFE;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="long" vertex="1">
          <mxGeometry height="30" width="120" x="60" y="275" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="Long" vertex="1">
          <mxGeometry height="30" width="140" x="180" y="275" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=11;fontColor=#1E40AF;" value="Number → Object" vertex="1">
          <mxGeometry height="30" width="200" x="320" y="275" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="数值型" vertex="1">
          <mxGeometry height="30" width="120" x="520" y="275" as="geometry" />
        </mxCell>

        <mxCell id="26" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#92400E;fontFamily=Courier New;" value="float" vertex="1">
          <mxGeometry height="30" width="120" x="60" y="305" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#92400E;fontFamily=Courier New;" value="Float" vertex="1">
          <mxGeometry height="30" width="140" x="180" y="305" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=11;fontColor=#92400E;" value="Number → Object" vertex="1">
          <mxGeometry height="30" width="200" x="320" y="305" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="数值型" vertex="1">
          <mxGeometry height="30" width="120" x="520" y="305" as="geometry" />
        </mxCell>

        <mxCell id="30" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#92400E;fontFamily=Courier New;" value="double" vertex="1">
          <mxGeometry height="30" width="120" x="60" y="335" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#92400E;fontFamily=Courier New;" value="Double" vertex="1">
          <mxGeometry height="30" width="140" x="180" y="335" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=11;fontColor=#92400E;" value="Number → Object" vertex="1">
          <mxGeometry height="30" width="200" x="320" y="335" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="数值型" vertex="1">
          <mxGeometry height="30" width="120" x="520" y="335" as="geometry" />
        </mxCell>

        <mxCell id="34" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#166534;fontFamily=Courier New;" value="char" vertex="1">
          <mxGeometry height="30" width="120" x="60" y="365" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#166534;fontFamily=Courier New;" value="Character" vertex="1">
          <mxGeometry height="30" width="140" x="180" y="365" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#BBF7D0;strokeWidth=1;fontSize=11;fontColor=#166534;" value="Object" vertex="1">
          <mxGeometry height="30" width="200" x="320" y="365" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="字符型" vertex="1">
          <mxGeometry height="30" width="120" x="520" y="365" as="geometry" />
        </mxCell>

        <mxCell id="38" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#E9D5FF;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#6D28D9;fontFamily=Courier New;" value="boolean" vertex="1">
          <mxGeometry height="30" width="120" x="60" y="395" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#E9D5FF;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#6D28D9;fontFamily=Courier New;" value="Boolean" vertex="1">
          <mxGeometry height="30" width="140" x="180" y="395" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#E9D5FF;strokeWidth=1;fontSize=11;fontColor=#6D28D9;" value="Object" vertex="1">
          <mxGeometry height="30" width="200" x="320" y="395" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#8B5CF6;strokeColor=#7C3AED;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="布尔型" vertex="1">
          <mxGeometry height="30" width="120" x="520" y="395" as="geometry" />
        </mxCell>

        <mxCell id="42" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#E2E8F0;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="255" width="185" x="660" y="150" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=#1E293B;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💡 说明" vertex="1">
          <mxGeometry height="22" width="60" x="675" y="160" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="6个数值类型" vertex="1">
          <mxGeometry height="20" width="80" x="675" y="195" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#475569;spacingLeft=5;" value="继承自 Number 类" vertex="1">
          <mxGeometry height="18" width="120" x="675" y="220" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Character" vertex="1">
          <mxGeometry height="20" width="70" x="675" y="250" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#475569;spacingLeft=5;" value="直接继承 Object" vertex="1">
          <mxGeometry height="18" width="110" x="675" y="275" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="Boolean" vertex="1">
          <mxGeometry height="20" width="60" x="675" y="305" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#475569;spacingLeft=5;" value="直接继承 Object" vertex="1">
          <mxGeometry height="18" width="110" x="675" y="330" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#991B1B;rounded=1;" value="所有包装类都是 final" vertex="1">
          <mxGeometry height="22" width="130" x="675" y="365" 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="28" width="140" x="40" y="450" as="geometry" />
        </mxCell>

        <mxCell id="52" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="330" width="820" x="40" y="490" as="geometry" />
        </mxCell>

        <mxCell id="53" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=2;fontSize=16;fontStyle=1;fontColor=#FFFFFF;" value="Object" vertex="1">
          <mxGeometry height="45" width="120" x="390" y="510" as="geometry" />
        </mxCell>

        <mxCell id="edge1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#64748B;strokeWidth=2;endArrow=block;endFill=0;endSize=10;" edge="1" source="53" target="59">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="450" y="575" />
              <mxPoint x="340" y="575" />
            </Array>
          </mxGeometry>
        </mxCell>
        <mxCell id="edge2" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#64748B;strokeWidth=2;endArrow=block;endFill=0;endSize=10;" edge="1" source="53" target="60">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="450" y="575" />
              <mxPoint x="550" y="575" />
            </Array>
          </mxGeometry>
        </mxCell>
        <mxCell id="edge3" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#64748B;strokeWidth=2;endArrow=block;endFill=0;endSize=10;" edge="1" source="53" target="61">
          <mxGeometry relative="1" as="geometry">
            <Array as="points">
              <mxPoint x="450" y="575" />
              <mxPoint x="730" y="575" />
            </Array>
          </mxGeometry>
        </mxCell>

        <mxCell id="59" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="Number&#xa;(abstract)" vertex="1">
          <mxGeometry height="50" width="160" x="260" y="600" as="geometry" />
        </mxCell>
        <mxCell id="60" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="Character" vertex="1">
          <mxGeometry height="50" width="120" x="490" y="600" as="geometry" />
        </mxCell>
        <mxCell id="61" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#8B5CF6;strokeColor=#7C3AED;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="Boolean" vertex="1">
          <mxGeometry height="50" width="120" x="670" y="600" as="geometry" />
        </mxCell>

        <mxCell id="line_num_main" parent="1" style="endArrow=none;html=1;strokeColor=#3B82F6;strokeWidth=2;" edge="1">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="340" y="650" as="sourcePoint" />
            <mxPoint x="340" y="680" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="line_num_cross" parent="1" style="endArrow=none;html=1;strokeColor=#3B82F6;strokeWidth=2;" edge="1">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="90" y="680" as="sourcePoint" />
            <mxPoint x="590" y="680" as="targetPoint" />
          </mxGeometry>
        </mxCell>

        <mxCell id="num1" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#1E40AF;" value="Byte" vertex="1">
          <mxGeometry height="40" width="80" x="50" y="710" as="geometry" />
        </mxCell>
        <mxCell id="edge_num1" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;strokeColor=#3B82F6;strokeWidth=2;endArrow=block;endFill=0;endSize=8;" edge="1" target="num1">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="90" y="680" as="sourcePoint" />
          </mxGeometry>
        </mxCell>

        <mxCell id="num2" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#1E40AF;" value="Short" vertex="1">
          <mxGeometry height="40" width="80" x="150" y="710" as="geometry" />
        </mxCell>
        <mxCell id="edge_num2" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;strokeColor=#3B82F6;strokeWidth=2;endArrow=block;endFill=0;endSize=8;" edge="1" target="num2">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="190" y="680" as="sourcePoint" />
          </mxGeometry>
        </mxCell>

        <mxCell id="num3" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#1E40AF;" value="Integer" vertex="1">
          <mxGeometry height="40" width="80" x="250" y="710" as="geometry" />
        </mxCell>
        <mxCell id="edge_num3" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;strokeColor=#3B82F6;strokeWidth=2;endArrow=block;endFill=0;endSize=8;" edge="1" target="num3">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="290" y="680" as="sourcePoint" />
          </mxGeometry>
        </mxCell>

        <mxCell id="num4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#1E40AF;" value="Long" vertex="1">
          <mxGeometry height="40" width="80" x="350" y="710" as="geometry" />
        </mxCell>
        <mxCell id="edge_num4" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;strokeColor=#3B82F6;strokeWidth=2;endArrow=block;endFill=0;endSize=8;" edge="1" target="num4">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="390" y="680" as="sourcePoint" />
          </mxGeometry>
        </mxCell>

        <mxCell id="num5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#92400E;" value="Float" vertex="1">
          <mxGeometry height="40" width="80" x="450" y="710" as="geometry" />
        </mxCell>
        <mxCell id="edge_num5" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;strokeColor=#3B82F6;strokeWidth=2;endArrow=block;endFill=0;endSize=8;" edge="1" target="num5">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="490" y="680" as="sourcePoint" />
          </mxGeometry>
        </mxCell>

        <mxCell id="num6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#92400E;" value="Double" vertex="1">
          <mxGeometry height="40" width="80" x="550" y="710" as="geometry" />
        </mxCell>
        <mxCell id="edge_num6" parent="1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;html=1;strokeColor=#3B82F6;strokeWidth=2;endArrow=block;endFill=0;endSize=8;" edge="1" target="num6">
          <mxGeometry relative="1" as="geometry">
            <mxPoint x="590" y="680" as="sourcePoint" />
          </mxGeometry>
        </mxCell>

      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

7.2 基本类型与包装类的区别

<mxfile>
  <diagram id="primitive-vs-wrapper" name="基本类型 vs 包装类型">
    <mxGraphModel dx="926" dy="817" 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=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="720" width="820" x="40" y="20" 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=26;fontStyle=1;fontColor=#1a237e;" value="基本类型 vs 包装类型" vertex="1">
          <mxGeometry height="50" width="400" x="250" y="40" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3949ab;strokeColor=none;arcSize=8;" value="" vertex="1">
          <mxGeometry height="55" width="760" x="70" y="105" 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=17;fontStyle=1;fontColor=#ffffff;" value="对比项" vertex="1">
          <mxGeometry height="55" width="160" x="70" y="105" 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=17;fontStyle=1;fontColor=#ffffff;" value="基本类型" vertex="1">
          <mxGeometry height="55" width="250" x="230" y="105" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=17;fontStyle=1;fontColor=#ffffff;" value="包装类型" vertex="1">
          <mxGeometry height="55" width="350" x="480" y="105" as="geometry" />
        </mxCell>
        <mxCell id="8" edge="1" parent="1" style="endArrow=none;html=1;strokeColor=#e0e0e0;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="230" y="160" as="sourcePoint" />
            <mxPoint x="230" y="690" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="9" edge="1" parent="1" style="endArrow=none;html=1;strokeColor=#e0e0e0;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="480" y="160" as="sourcePoint" />
            <mxPoint x="480" y="690" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="55" width="760" x="70" y="160" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#1565c0;" value="存储内容" vertex="1">
          <mxGeometry height="55" width="160" x="70" y="160" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="直接存储值" vertex="1">
          <mxGeometry height="55" width="250" x="230" y="160" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="存储对象引用" vertex="1">
          <mxGeometry height="55" width="350" x="480" y="160" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="70" width="760" x="70" y="215" 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=14;fontStyle=1;fontColor=#1565c0;" value="存储位置" vertex="1">
          <mxGeometry height="70" width="160" x="70" y="215" 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=14;" value="&lt;font color=&quot;#2e7d32&quot;&gt;栈&lt;/font&gt;(局部变量)&lt;br&gt;&lt;font color=&quot;#1565c0&quot;&gt;堆&lt;/font&gt;(成员变量)" vertex="1">
          <mxGeometry height="70" width="250" x="230" y="215" 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;" value="&lt;font color=&quot;#1565c0&quot;&gt;堆&lt;/font&gt;(对象本身)&lt;br&gt;&lt;font color=&quot;#2e7d32&quot;&gt;栈&lt;/font&gt;/&lt;font color=&quot;#1565c0&quot;&gt;堆&lt;/font&gt;(引用)" vertex="1">
          <mxGeometry height="70" width="350" x="480" y="215" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="55" width="760" x="70" y="285" 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;fontColor=#1565c0;" value="默认值" vertex="1">
          <mxGeometry height="55" width="160" x="70" y="285" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#4caf50;" value="" vertex="1">
          <mxGeometry height="30" width="160" x="275" y="297" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#2e7d32;" value="&lt;font face=&quot;Courier New&quot;&gt;0 / 0.0 / false&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="160" x="275" y="297" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#e57373;" value="" vertex="1">
          <mxGeometry height="30" width="100" x="595" y="297" 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=13;fontColor=#c62828;" value="&lt;font face=&quot;Courier New&quot;&gt;null&lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="595" y="297" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="70" width="760" x="70" y="340" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#1565c0;" value="空间占用" vertex="1">
          <mxGeometry height="70" width="160" x="70" y="340" 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=14;" value="固定大小&lt;br&gt;&lt;font color=&quot;#757575&quot;&gt;(1~8 字节)&lt;/font&gt;" vertex="1">
          <mxGeometry height="70" width="250" x="230" y="340" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="对象头 + 值 + 对齐填充&lt;br&gt;&lt;font color=&quot;#757575&quot;&gt;(至少 16 字节)&lt;/font&gt;" vertex="1">
          <mxGeometry height="70" width="350" x="480" y="340" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="70" width="760" x="70" y="410" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#1565c0;" value="比较方式" vertex="1">
          <mxGeometry height="70" width="160" x="70" y="410" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#81c784;" value="" vertex="1">
          <mxGeometry height="30" width="150" x="280" y="430" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font face=&quot;Courier New&quot;&gt;==&lt;/font&gt; 比较值" vertex="1">
          <mxGeometry height="30" width="150" x="280" y="430" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font face=&quot;Courier New&quot;&gt;==&lt;/font&gt; 比较地址&lt;br&gt;&lt;font face=&quot;Courier New&quot;&gt;equals()&lt;/font&gt; 比较值" vertex="1">
          <mxGeometry height="70" width="350" x="480" y="410" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="55" width="760" x="70" y="480" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#1565c0;" value="泛型支持" vertex="1">
          <mxGeometry height="55" width="160" x="70" y="480" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#e57373;" value="" vertex="1">
          <mxGeometry height="30" width="120" x="295" y="492" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#c62828;" value="❌ 不支持" vertex="1">
          <mxGeometry height="30" width="120" x="295" y="492" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#81c784;" value="" vertex="1">
          <mxGeometry height="30" width="120" x="595" y="492" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#2e7d32;" value="✅ 支持" vertex="1">
          <mxGeometry height="30" width="120" x="595" y="492" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="55" width="760" x="70" y="535" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#1565c0;" value="集合使用" vertex="1">
          <mxGeometry height="55" width="160" x="70" y="535" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#e57373;" value="" vertex="1">
          <mxGeometry height="30" width="160" x="275" y="547" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#c62828;" value="❌ 不能放入集合" vertex="1">
          <mxGeometry height="30" width="160" x="275" y="547" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#81c784;" value="" vertex="1">
          <mxGeometry height="30" width="160" x="575" y="547" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#2e7d32;" value="✅ 可以放入集合" vertex="1">
          <mxGeometry height="30" width="160" x="575" y="547" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="55" width="760" x="70" y="590" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontStyle=1;fontColor=#1565c0;" value="空值表示" vertex="1">
          <mxGeometry height="55" width="160" x="70" y="590" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#e57373;" value="" vertex="1">
          <mxGeometry height="30" width="150" x="280" y="602" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#c62828;" value="❌ 无法表示空" vertex="1">
          <mxGeometry height="30" width="150" x="280" y="602" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#81c784;" value="" vertex="1">
          <mxGeometry height="30" width="150" x="580" y="602" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#2e7d32;" value="✅ 可以为 null" vertex="1">
          <mxGeometry height="30" width="150" x="580" y="602" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="40" width="760" x="70" y="660" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#e65100;fontStyle=1;" value="💡 选择建议:性能敏感用基本类型,需要对象特性(泛型/集合/null)用包装类型" vertex="1">
          <mxGeometry height="40" width="760" x="70" y="660" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
public class WrapperDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // 默认值差异
        // ═══════════════════════════════════════════════════════════════

        int primitiveInt;      // 局部变量必须初始化
        Integer wrapperInt = null;  // 可以为 null

        // ═══════════════════════════════════════════════════════════════
        // 比较方式差异
        // ═══════════════════════════════════════════════════════════════

        int a = 100;
        int b = 100;
        System.out.println(a == b);  // true,比较值

        Integer c = 200;
        Integer d = 200;
        System.out.println(c == d);       // false,比较地址
        System.out.println(c.equals(d));  // true,比较值

        // ═══════════════════════════════════════════════════════════════
        // 泛型与集合
        // ═══════════════════════════════════════════════════════════════

        // List<int> list;  // ❌ 编译错误
        List<Integer> list = new ArrayList<>();  // ✅ 正确
        list.add(1);  // 自动装箱

        // ═══════════════════════════════════════════════════════════════
        // 空值表示场景
        // ═══════════════════════════════════════════════════════════════

        // 数据库查询结果可能为 null
        Integer score = getScoreFromDB();  // 可能返回 null
        if (score == null) {
            System.out.println("成绩未录入");
        }
    }
}

7.3 自动装箱与拆箱

<mxfile>
  <diagram id="autoboxing-unboxing" name="自动装箱与拆箱">
    <mxGraphModel dx="1111" dy="980" 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=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="680" width="820" x="40" y="20" 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=28;fontStyle=1;fontColor=#1a237e;" value="自动装箱与拆箱" vertex="1">
          <mxGeometry height="50" width="340" x="280" y="40" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="245" width="780" x="60" y="105" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="45" width="780" x="60" y="105" 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=18;fontStyle=1;fontColor=#ffffff;" value="📦 装箱 (Boxing):基本类型 → 包装类型" vertex="1">
          <mxGeometry height="45" width="780" x="60" y="105" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;shadow=1;" value="" vertex="1">
          <mxGeometry height="60" width="140" x="90" y="170" 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=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;基本类型&lt;/b&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot;&gt;int value = 100&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="140" x="90" y="170" as="geometry" />
        </mxCell>
        <mxCell id="9" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#1976d2;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="230" y="200" as="sourcePoint" />
            <mxPoint x="290" y="200" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#1976d2;fontStyle=1;" value="自动装箱" vertex="1">
          <mxGeometry height="20" width="60" x="230" y="175" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=none;shadow=1;" value="" vertex="1">
          <mxGeometry height="60" width="180" x="290" y="170" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;赋值语句&lt;/b&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot;&gt;Integer obj = value;&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="180" x="290" y="170" as="geometry" />
        </mxCell>
        <mxCell id="13" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#1976d2;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="470" y="200" as="sourcePoint" />
            <mxPoint x="530" y="200" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#1976d2;fontStyle=1;" value="编译转换" vertex="1">
          <mxGeometry height="20" width="60" x="470" y="175" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7b1fa2;strokeColor=none;shadow=1;" value="" vertex="1">
          <mxGeometry height="60" width="290" x="530" y="170" 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=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;包装类型&lt;/b&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot;&gt;Integer obj = Integer.valueOf(value);&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="290" x="530" y="170" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#90caf9;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="85" width="730" x="90" y="250" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;spacingLeft=15;" value="&lt;div style=&quot;text-align: left;&quot;&gt;&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#9e9e9e&quot;&gt;// 源代码&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#0d47a1&quot;&gt;int&lt;/font&gt; value = &lt;font color=&quot;#e65100&quot;&gt;100&lt;/font&gt;;&lt;br&gt;&lt;font color=&quot;#7b1fa2&quot;&gt;Integer&lt;/font&gt; obj = value;&amp;nbsp; &lt;font color=&quot;#9e9e9e&quot;&gt;// 自动装箱 → 编译后: Integer.valueOf(value)&lt;/font&gt;&lt;/div&gt;" vertex="1">
          <mxGeometry height="85" width="730" x="90" y="250" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fce4ec;strokeColor=#c2185b;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="245" width="780" x="60" y="370" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c2185b;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="45" width="780" x="60" y="370" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1;fontColor=#ffffff;" value="📤 拆箱 (Unboxing):包装类型 → 基本类型" vertex="1">
          <mxGeometry height="45" width="780" x="60" y="370" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#7b1fa2;strokeColor=none;shadow=1;" value="" vertex="1">
          <mxGeometry height="60" width="200" x="90" y="435" 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=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;包装类型&lt;/b&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot;&gt;Integer obj = ...&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="200" x="90" y="435" as="geometry" />
        </mxCell>
        <mxCell id="24" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#c2185b;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="290" y="465" as="sourcePoint" />
            <mxPoint x="350" y="465" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#c2185b;fontStyle=1;" value="自动拆箱" vertex="1">
          <mxGeometry height="20" width="60" x="290" y="440" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=none;shadow=1;" value="" vertex="1">
          <mxGeometry height="60" width="170" x="350" y="435" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;赋值语句&lt;/b&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot;&gt;int value = obj;&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="170" x="350" y="435" as="geometry" />
        </mxCell>
        <mxCell id="28" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#c2185b;strokeWidth=3;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="520" y="465" as="sourcePoint" />
            <mxPoint x="580" y="465" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#c2185b;fontStyle=1;" value="编译转换" vertex="1">
          <mxGeometry height="20" width="60" x="520" y="440" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;shadow=1;" value="" vertex="1">
          <mxGeometry height="60" width="240" x="580" y="435" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;基本类型&lt;/b&gt;&lt;br&gt;&lt;font face=&quot;Courier New&quot;&gt;int value = obj.intValue();&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="240" x="580" y="435" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#f48fb1;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="85" width="730" x="90" y="515" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;spacingLeft=15;" value="&lt;div style=&quot;text-align: left;&quot;&gt;&lt;font face=&quot;Courier New&quot;&gt;&lt;font color=&quot;#9e9e9e&quot;&gt;// 源代码&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#7b1fa2&quot;&gt;Integer&lt;/font&gt; obj = &lt;font color=&quot;#7b1fa2&quot;&gt;Integer&lt;/font&gt;.valueOf(&lt;font color=&quot;#e65100&quot;&gt;100&lt;/font&gt;);&lt;br&gt;&lt;font color=&quot;#0d47a1&quot;&gt;int&lt;/font&gt; value = obj;&amp;nbsp; &lt;font color=&quot;#9e9e9e&quot;&gt;// 自动拆箱 → 编译后: obj.intValue()&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;" vertex="1">
          <mxGeometry height="85" width="730" x="90" y="515" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff8e1;strokeColor=#ffa000;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="30" width="780" x="60" y="625" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#e65100;fontStyle=1;" value="💡 自动装箱/拆箱是编译器的语法糖,运行时仍调用 valueOf() 和 xxxValue() 方法" vertex="1">
          <mxGeometry height="30" width="780" x="60" y="625" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
public class BoxingDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // 自动装箱
        // ═══════════════════════════════════════════════════════════════

        Integer a = 100;        // Integer.valueOf(100)
        Double b = 3.14;        // Double.valueOf(3.14)
        Boolean c = true;       // Boolean.valueOf(true)

        // ═══════════════════════════════════════════════════════════════
        // 自动拆箱
        // ═══════════════════════════════════════════════════════════════

        int x = a;              // a.intValue()
        double y = b;           // b.doubleValue()
        boolean z = c;          // c.booleanValue()

        // ═══════════════════════════════════════════════════════════════
        // 运算中的自动拆箱
        // ═══════════════════════════════════════════════════════════════

        Integer num1 = 10;
        Integer num2 = 20;
        int sum = num1 + num2;  // 先拆箱再相加

        // 等价于:
        // int sum = num1.intValue() + num2.intValue();

        // ═══════════════════════════════════════════════════════════════
        // 比较中的陷阱
        // ═══════════════════════════════════════════════════════════════

        Integer i1 = 100;
        Integer i2 = 100;
        Integer i3 = 200;
        Integer i4 = 200;

        System.out.println(i1 == i2);  // true  (缓存)
        System.out.println(i3 == i4);  // false (超出缓存)

        // 与基本类型比较时会自动拆箱
        int p = 100;
        System.out.println(i1 == p);   // true (i1 拆箱后比较值)
    }
}

自动拆箱的 NPE 陷阱

public class NullPointerDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // 拆箱 null 会抛出 NullPointerException
        // ═══════════════════════════════════════════════════════════════

        Integer num = null;

        // int value = num;  // ❌ NullPointerException
        // 等价于: int value = num.intValue();  // null.intValue() 报错

        // 安全写法
        int value = (num != null) ? num : 0;
        // 或 JDK 9+
        int value2 = Objects.requireNonNullElse(num, 0);

        // ═══════════════════════════════════════════════════════════════
        // 三元运算符的类型统一陷阱
        // ═══════════════════════════════════════════════════════════════

        Integer a = null;
        Integer b = 10;

        // 三元表达式要求两边类型一致
        // 如果一边是基本类型,另一边会拆箱
        Integer result = (a != null) ? a : 0;  // OK
        // Integer result2 = true ? null : 0;  // NPE! null 被拆箱
    }
}

7.4 包装类缓存机制

<mxfile>
  <diagram id="wrapper-cache-mechanism" name="包装类缓存机制">
    <mxGraphModel dx="1068" dy="484" 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="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=28;fontStyle=1;fontColor=#1E293B;" value="包装类缓存机制" vertex="1">
          <mxGeometry height="45" width="400" x="250" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=13;fontColor=#FFFFFF;rounded=1;" value="JVM 对部分包装类实现了缓存,以节省内存和提高性能" vertex="1">
          <mxGeometry height="30" width="500" x="200" y="65" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#E2E8F0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="380" width="820" x="40" y="115" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="包装类" vertex="1">
          <mxGeometry height="40" width="140" x="60" y="135" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="缓存范围" vertex="1">
          <mxGeometry height="40" width="160" x="200" y="135" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="说明" vertex="1">
          <mxGeometry height="40" width="280" x="360" y="135" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="缓存状态" vertex="1">
          <mxGeometry height="41" width="100" x="640" y="134" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#BFDBFE;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="Byte" vertex="1">
          <mxGeometry height="38" width="140" x="60" y="175" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="-128 ~ 127" vertex="1">
          <mxGeometry height="38" width="160" x="200" y="175" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=12;fontColor=#1E40AF;" value="全部缓存(byte 整个范围)" vertex="1">
          <mxGeometry height="38" width="280" x="360" y="175" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="✓ 全缓存" vertex="1">
          <mxGeometry height="24" width="80" x="650" y="182" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#BFDBFE;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="Short" vertex="1">
          <mxGeometry height="38" width="140" x="60" y="213" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="-128 ~ 127" vertex="1">
          <mxGeometry height="38" width="160" x="200" y="213" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=12;fontColor=#1E40AF;" value="部分缓存" vertex="1">
          <mxGeometry height="38" width="280" x="360" y="213" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="◐ 部分" vertex="1">
          <mxGeometry height="24" width="80" x="650" y="220" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#FDE68A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#92400E;fontFamily=Courier New;" value="Integer" vertex="1">
          <mxGeometry height="38" width="140" x="60" y="251" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#92400E;fontFamily=Courier New;" value="-128 ~ 127" vertex="1">
          <mxGeometry height="38" width="160" x="200" y="251" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontStyle=1;fontColor=#92400E;" value="可通过 JVM 参数扩展上限 ⭐" vertex="1">
          <mxGeometry height="38" width="280" x="360" y="251" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F59E0B;strokeColor=#D97706;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="◐ 可扩展" vertex="1">
          <mxGeometry height="24" width="80" x="650" y="258" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#BFDBFE;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="Long" vertex="1">
          <mxGeometry height="38" width="140" x="60" y="289" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" value="-128 ~ 127" vertex="1">
          <mxGeometry height="38" width="160" x="200" y="289" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=12;fontColor=#1E40AF;" value="部分缓存" vertex="1">
          <mxGeometry height="38" width="280" x="360" y="289" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#3B82F6;strokeColor=#2563EB;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="◐ 部分" vertex="1">
          <mxGeometry height="24" width="80" x="650" y="296" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#BBF7D0;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#166534;fontFamily=Courier New;" value="Character" vertex="1">
          <mxGeometry height="38" width="140" x="60" y="327" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#BBF7D0;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#166534;fontFamily=Courier New;" value="0 ~ 127" vertex="1">
          <mxGeometry height="38" width="160" x="200" y="327" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontColor=#166534;" value="ASCII 字符范围" vertex="1">
          <mxGeometry height="38" width="280" x="360" y="327" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="◐ 部分" vertex="1">
          <mxGeometry height="24" width="80" x="650" y="334" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#E9D5FF;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#6D28D9;fontFamily=Courier New;" value="Boolean" vertex="1">
          <mxGeometry height="38" width="140" x="60" y="365" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#E9D5FF;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#6D28D9;fontFamily=Courier New;" value="true / false" vertex="1">
          <mxGeometry height="38" width="160" x="200" y="365" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#E9D5FF;strokeWidth=1;fontSize=12;fontColor=#6D28D9;" value="仅两个实例(单例模式)" vertex="1">
          <mxGeometry height="38" width="280" x="360" y="365" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#8B5CF6;strokeColor=#7C3AED;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="✓ 全缓存" vertex="1">
          <mxGeometry height="24" width="80" x="650" y="372" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#FECACA;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#991B1B;fontFamily=Courier New;" value="Float" vertex="1">
          <mxGeometry height="38" width="140" x="60" y="403" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#991B1B;" value="无缓存" vertex="1">
          <mxGeometry height="38" width="160" x="200" y="403" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=1;fontSize=12;fontColor=#991B1B;" value="浮点数范围太大,无法有效缓存" vertex="1">
          <mxGeometry height="38" width="280" x="360" y="403" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="✗ 无" vertex="1">
          <mxGeometry height="24" width="80" x="650" y="410" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#FECACA;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#991B1B;fontFamily=Courier New;" value="Double" vertex="1">
          <mxGeometry height="38" width="140" x="60" y="441" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#991B1B;" value="无缓存" vertex="1">
          <mxGeometry height="38" width="160" x="200" y="441" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=1;fontSize=12;fontColor=#991B1B;" value="浮点数范围太大,无法有效缓存" vertex="1">
          <mxGeometry height="38" width="280" x="360" y="441" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontSize=11;fontStyle=1;fontColor=#FFFFFF;" value="✗ 无" vertex="1">
          <mxGeometry height="24" width="80" x="650" y="448" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="180" width="85" x="755" y="215" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="💡 提示" vertex="1">
          <mxGeometry height="22" width="60" x="765" y="225" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#92400E;" value="Integer 缓存&lt;br&gt;上限可配置" vertex="1">
          <mxGeometry height="35" width="75" x="760" y="255" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=9;fontFamily=Courier New;fontColor=#92400E;rounded=1;" value="-XX:AutoBox&lt;br&gt;CacheMax" vertex="1">
          <mxGeometry height="35" width="75" x="760" y="295" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=10;fontColor=#92400E;" value="例如设为&lt;br&gt;1000" vertex="1">
          <mxGeometry height="30" width="75" x="760" y="335" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=9;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="缓存 -128~1000" vertex="1">
          <mxGeometry height="20" width="80" x="757" y="368" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#991B1B;rounded=1;" value="⚠️ 注意:缓存范围内的值使用 == 比较返回 true,范围外返回 false,建议始终使用 equals() 比较包装类" vertex="1">
          <mxGeometry height="32" width="700" x="100" y="510" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="170" width="820" x="40" y="560" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📝 代码示例" vertex="1">
          <mxGeometry height="26" width="100" x="60" y="575" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=left;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontColor=#166534;rounded=1;spacingLeft=10;" value="Integer a = 127;&lt;br&gt;Integer b = 127;&lt;br&gt;System.out.println(a == b);" vertex="1">
          <mxGeometry height="55" width="220" x="60" y="615" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="// true(缓存范围内)" vertex="1">
          <mxGeometry height="24" width="160" x="60" y="680" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=left;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontColor=#991B1B;rounded=1;spacingLeft=10;" value="Integer c = 128;&lt;br&gt;Integer d = 128;&lt;br&gt;System.out.println(c == d);" vertex="1">
          <mxGeometry height="55" width="220" x="310" y="615" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="// false(超出缓存范围)" vertex="1">
          <mxGeometry height="24" width="175" x="310" y="680" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=left;verticalAlign=middle;fontSize=12;fontFamily=Courier New;fontColor=#1E40AF;rounded=1;spacingLeft=10;" value="Integer e = 128;&lt;br&gt;Integer f = 128;&lt;br&gt;System.out.println(e.equals(f));" vertex="1">
          <mxGeometry height="55" width="280" x="560" y="615" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="// true(推荐写法 ✓)" vertex="1">
          <mxGeometry height="24" width="145" x="560" y="680" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

Integer 缓存源码分析

// Integer.java 源码(JDK 8+)
public final class Integer extends Number implements Comparable<Integer> {

    // valueOf 方法使用缓存
    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;         // 最大值可配置
        static final Integer[] cache;  // 缓存数组

        static {
            // 默认最大值 127,可通过 JVM 参数调整
            int h = 127;
            String integerCacheHighPropValue =
                VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);  // 至少 127
                h = Math.min(i, Integer.MAX_VALUE - (-low) - 1);
            }
            high = h;

            // 创建缓存数组
            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-cache" name="Integer 缓存机制图解">
    <mxGraphModel dx="926" dy="817" 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=#fafafa;strokeColor=#5c6bc0;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="720" width="820" x="40" y="20" 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=26;fontStyle=1;fontColor=#1a237e;" value="Integer 缓存机制图解" vertex="1">
          <mxGeometry height="45" width="400" x="250" y="35" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontColor=#5c6bc0;" value="Integer.valueOf(100) 的执行过程" vertex="1">
          <mxGeometry height="25" width="340" x="280" y="75" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#3f51b5;strokeWidth=2;dashed=1;" value="" vertex="1">
          <mxGeometry height="160" width="780" x="60" y="115" 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=#3f51b5;" value="IntegerCache.cache (静态数组,JVM启动时初始化)" vertex="1">
          <mxGeometry height="30" width="780" x="60" y="120" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c5cae9;strokeColor=#3f51b5;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="70" x="80" y="160" 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=13;" value="&lt;b&gt;-128&lt;/b&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 11px;&quot;&gt;[0]&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="70" x="80" y="160" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c5cae9;strokeColor=#3f51b5;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="70" x="150" y="160" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;b&gt;-127&lt;/b&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 11px;&quot;&gt;[1]&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="70" x="150" y="160" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;strokeDasharray=3 3;" value="" vertex="1">
          <mxGeometry height="50" width="60" x="220" y="160" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;" value="&lt;font color=&quot;#9e9e9e&quot;&gt;...&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="60" x="220" y="160" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c5cae9;strokeColor=#3f51b5;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="70" x="280" y="160" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;b&gt;0&lt;/b&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 11px;&quot;&gt;[128]&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="70" x="280" y="160" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;strokeDasharray=3 3;" value="" vertex="1">
          <mxGeometry height="50" width="60" x="350" y="160" 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=16;" value="&lt;font color=&quot;#9e9e9e&quot;&gt;...&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="60" x="350" y="160" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=#2e7d32;strokeWidth=3;" value="" vertex="1">
          <mxGeometry height="50" width="80" x="410" y="160" 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=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;100&lt;/b&gt;&lt;br&gt;&lt;span style=&quot;font-size: 11px;&quot;&gt;[228]&lt;/span&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="80" x="410" y="160" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#9fa8da;strokeWidth=1;strokeDasharray=3 3;" value="" vertex="1">
          <mxGeometry height="50" width="60" x="490" y="160" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=16;" value="&lt;font color=&quot;#9e9e9e&quot;&gt;...&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="60" x="490" y="160" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#c5cae9;strokeColor=#3f51b5;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="70" x="550" y="160" 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=13;" value="&lt;b&gt;127&lt;/b&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 11px;&quot;&gt;[255]&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="70" x="550" y="160" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="180" x="640" y="155" 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=13;" value="📌 缓存范围&lt;br&gt;&lt;b&gt;-128 ~ 127&lt;/b&gt;&lt;br&gt;&lt;font style=&quot;font-size: 11px;&quot; color=&quot;#757575&quot;&gt;共 256 个对象&lt;/font&gt;" vertex="1">
          <mxGeometry height="60" width="180" x="640" y="155" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#5c6bc0;" value="&lt;font face=&quot;Courier New&quot;&gt;index = value + 128&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="200" x="200" y="220" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="175" width="780" x="60" y="290" 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=15;fontStyle=1;fontColor=#2e7d32;spacingLeft=10;" value="✅ 缓存命中 (值在 -128 ~ 127 范围内)" vertex="1">
          <mxGeometry height="30" width="400" x="60" y="295" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#81c784;strokeColor=#4caf50;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="180" x="90" y="335" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#1b5e20;" value="&lt;font face=&quot;Courier New&quot;&gt;Integer a = 100;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="180" x="90" y="335" as="geometry" />
        </mxCell>
        <mxCell id="30" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#4caf50;strokeWidth=2;entryX=0;entryY=0.5;entryDx=0;entryDy=0;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="270" y="357" as="sourcePoint" />
            <mxPoint x="410" y="357" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#81c784;strokeColor=#4caf50;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="180" x="90" y="390" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#1b5e20;" value="&lt;font face=&quot;Courier New&quot;&gt;Integer b = 100;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="180" x="90" y="390" as="geometry" />
        </mxCell>
        <mxCell id="33" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#4caf50;strokeWidth=2;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="270" y="412" as="sourcePoint" />
            <mxPoint x="410" y="380" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=#2e7d32;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="70" width="140" x="410" y="340" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;cache[228]&lt;/b&gt;&lt;br&gt;Integer(100)&lt;br&gt;&lt;font style=&quot;font-size: 11px;&quot;&gt;@0x1234&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="70" width="140" x="410" y="340" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#2e7d32;" value="返回同一个&lt;br&gt;缓存对象" vertex="1">
          <mxGeometry height="40" width="80" x="310" y="355" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#4caf50;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="230" x="590" y="350" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot;&gt;a == b&lt;/font&gt;  →  &lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;true&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 12px;&quot;&gt;(同一个对象,地址相同)&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="230" x="590" y="350" as="geometry" />
        </mxCell>
        <mxCell id="39" edge="1" parent="1" style="endArrow=none;html=1;strokeColor=#bdbdbd;strokeWidth=2;dashed=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="80" y="485" as="sourcePoint" />
            <mxPoint x="820" y="485" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffebee;strokeColor=#e53935;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="200" width="780" x="60" y="505" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontStyle=1;fontColor=#c62828;spacingLeft=10;" value="❌ 缓存未命中 (值超出 -128 ~ 127 范围)" vertex="1">
          <mxGeometry height="30" width="400" x="60" y="510" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ef9a9a;strokeColor=#e53935;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="180" x="90" y="555" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#b71c1c;" value="&lt;font face=&quot;Courier New&quot;&gt;Integer c = 200;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="180" x="90" y="555" as="geometry" />
        </mxCell>
        <mxCell id="44" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#e53935;strokeWidth=2;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="270" y="577" as="sourcePoint" />
            <mxPoint x="390" y="577" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#e53935;fontStyle=1;" value="new" vertex="1">
          <mxGeometry height="20" width="40" x="310" y="555" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e53935;strokeColor=#b71c1c;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="55" width="130" x="390" y="550" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;Integer(200)&lt;/b&gt;&lt;br&gt;&lt;font style=&quot;font-size: 11px;&quot;&gt;@0x5678&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="55" width="130" x="390" y="550" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ef9a9a;strokeColor=#e53935;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="45" width="180" x="90" y="615" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#b71c1c;" value="&lt;font face=&quot;Courier New&quot;&gt;Integer d = 200;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="180" x="90" y="615" as="geometry" />
        </mxCell>
        <mxCell id="50" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#e53935;strokeWidth=2;flowAnimation=1;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="270" y="637" as="sourcePoint" />
            <mxPoint x="390" y="637" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;fontColor=#e53935;fontStyle=1;" value="new" vertex="1">
          <mxGeometry height="20" width="40" x="310" y="615" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e53935;strokeColor=#b71c1c;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="55" width="130" x="390" y="615" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;Integer(200)&lt;/b&gt;&lt;br&gt;&lt;font style=&quot;font-size: 11px;&quot;&gt;@0x9ABC&lt;/font&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="55" width="130" x="390" y="615" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#c62828;" value="每次都创建&lt;br&gt;新对象" vertex="1">
          <mxGeometry height="40" width="70" x="520" y="595" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=28;fontStyle=1;fontColor=#e53935;" value="≠" vertex="1">
          <mxGeometry height="30" width="30" x="440" y="600" as="geometry" />
        </mxCell>
        <mxCell id="56" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#e53935;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="50" width="230" x="590" y="580" as="geometry" />
        </mxCell>
        <mxCell id="57" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot;&gt;c == d&lt;/font&gt;  →  &lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;false&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 12px;&quot;&gt;(不同对象,地址不同)&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="230" x="590" y="580" as="geometry" />
        </mxCell>
        <mxCell id="58" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff8e1;strokeColor=#ffa000;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="30" width="780" x="50" y="750" as="geometry" />
        </mxCell>
        <mxCell id="59" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#e65100;fontStyle=1;" value="💡 最佳实践:比较包装类型的值时,始终使用 equals() 方法,避免 == 带来的陷阱" vertex="1">
          <mxGeometry height="30" width="780" x="50" y="750" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>
public class CacheDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // 缓存范围内
        // ═══════════════════════════════════════════════════════════════

        Integer a = 127;
        Integer b = 127;
        System.out.println("a == b: " + (a == b));  // true

        Integer c = -128;
        Integer d = -128;
        System.out.println("c == d: " + (c == d));  // true

        // ═══════════════════════════════════════════════════════════════
        // 超出缓存范围
        // ═══════════════════════════════════════════════════════════════

        Integer e = 128;
        Integer f = 128;
        System.out.println("e == f: " + (e == f));  // false

        Integer g = -129;
        Integer h = -129;
        System.out.println("g == h: " + (g == h));  // false

        // ═══════════════════════════════════════════════════════════════
        // new 强制创建新对象(绕过缓存)
        // ═══════════════════════════════════════════════════════════════

        Integer i = new Integer(100);  // 直接 new,不使用缓存
        Integer j = Integer.valueOf(100);  // 使用缓存
        System.out.println("i == j: " + (i == j));  // false

        // ⚠️ 从 JDK 9 开始,new Integer() 被标记为 @Deprecated
        // 推荐使用 Integer.valueOf()

        // ═══════════════════════════════════════════════════════════════
        // Boolean 缓存
        // ═══════════════════════════════════════════════════════════════

        Boolean t1 = true;
        Boolean t2 = Boolean.valueOf(true);
        Boolean t3 = Boolean.TRUE;
        System.out.println("t1 == t2: " + (t1 == t2));  // true
        System.out.println("t1 == t3: " + (t1 == t3));  // true
    }
}

// JVM 参数调整 Integer 缓存上限
// java -XX:AutoBoxCacheMax=1000 CacheDemo

7.5 装箱拆箱的性能影响

public class BoxingPerformanceDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // 性能陷阱:频繁装箱拆箱
        // ═══════════════════════════════════════════════════════════════

        // ❌ 错误写法:Long 导致大量装箱
        long startBad = System.currentTimeMillis();
        Long sumBad = 0L;  // 使用包装类型
        for (long i = 0; i <= 1_000_000_000L; i++) {
            sumBad += i;  // 每次都要拆箱、相加、装箱
        }
        long endBad = System.currentTimeMillis();
        System.out.println("包装类型耗时: " + (endBad - startBad) + "ms");

        // ✅ 正确写法:使用基本类型
        long startGood = System.currentTimeMillis();
        long sumGood = 0L;  // 使用基本类型
        for (long i = 0; i <= 1_000_000_000L; i++) {
            sumGood += i;  // 纯基本类型运算
        }
        long endGood = System.currentTimeMillis();
        System.out.println("基本类型耗时: " + (endGood - startGood) + "ms");

        // 结果对比(示例):
        // 包装类型耗时: 约 6000ms
        // 基本类型耗时: 约 600ms
        // 性能差 10 倍!
    }
}
 <mxfile>
  <diagram id="boxing-best-practices-fixed" name="装箱拆箱最佳实践">
    <mxGraphModel dx="1068" dy="484" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="850" math="0" shadow="0">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#1E293B;" value="Java 装箱拆箱 (Boxing/Unboxing) 最佳实践" vertex="1">
          <mxGeometry height="45" width="500" x="200" y="20" as="geometry" />
        </mxCell>
        <mxCell id="3" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=13;fontColor=#FFFFFF;rounded=1;" value="基本类型 (Primitive) vs 包装类型 (Wrapper) 避坑指南" vertex="1">
          <mxGeometry height="28" width="320" x="300" y="65" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;arcSize=6;" value="" vertex="1">
          <mxGeometry height="280" width="400" x="40" y="110" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="✅ 推荐做法 (DO)" vertex="1">
          <mxGeometry height="32" width="140" x="40" y="110" as="geometry" />
        </mxCell>
        <mxCell id="6" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="55" width="370" x="55" y="155" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="1" vertex="1">
          <mxGeometry height="30" width="30" x="65" y="165" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#166534;fontStyle=1;spacingLeft=5;" value="局部变量、循环变量优先使用基本类型" vertex="1">
          <mxGeometry height="22" width="310" x="100" y="160" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#166534;rounded=1;spacingLeft=8;" value="int count = 0;  // ✓ 避免创建对象开销" vertex="1">
          <mxGeometry height="20" width="240" x="100" y="183" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="55" width="370" x="55" y="217" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="2" vertex="1">
          <mxGeometry height="30" width="30" x="65" y="227" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#166534;fontStyle=1;spacingLeft=5;" value="需要放入集合 (Collection) 时使用包装类" vertex="1">
          <mxGeometry height="22" width="280" x="100" y="222" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#166534;rounded=1;spacingLeft=8;" value="List&lt;Integer&gt; list; // 泛型不支持基本类型" vertex="1">
          <mxGeometry height="20" width="260" x="100" y="245" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="55" width="370" x="55" y="279" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="3" vertex="1">
          <mxGeometry height="30" width="30" x="65" y="289" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#166534;fontStyle=1;spacingLeft=5;" value="需要表示 &quot;无值/Null&quot; 语义时使用包装类" vertex="1">
          <mxGeometry height="22" width="280" x="100" y="284" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#166534;rounded=1;spacingLeft=8;" value="Integer age = null; // 表示&quot;未知&quot;" vertex="1">
          <mxGeometry height="20" width="220" x="100" y="307" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="370" x="55" y="341" as="geometry" />
        </mxCell>
        <mxCell id="19" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#22C55E;strokeColor=#16A34A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="4" vertex="1">
          <mxGeometry height="30" width="30" x="65" y="346" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#166534;fontStyle=1;spacingLeft=5;" value="包装类型比较永远使用 equals()" vertex="1">
          <mxGeometry height="22" width="280" x="100" y="350" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#EF4444;strokeWidth=2;arcSize=6;" value="" vertex="1">
          <mxGeometry height="280" width="400" x="460" y="110" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="❌ 避免做法 (DON&#39;T)" vertex="1">
          <mxGeometry height="32" width="160" x="460" y="110" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FECACA;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="70" width="370" x="475" y="155" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="1" vertex="1">
          <mxGeometry height="30" width="30" x="485" y="175" as="geometry" />
        </mxCell>
        <mxCell id="26" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#991B1B;fontStyle=1;spacingLeft=5;" value="循环体内使用包装类型做累加" vertex="1">
          <mxGeometry height="22" width="280" x="520" y="160" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#991B1B;rounded=1;spacingLeft=8;" value="Long sum = 0L;  // ✗ 定义为包装类" vertex="1">
          <mxGeometry height="18" width="230" x="520" y="183" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#991B1B;rounded=1;spacingLeft=8;" value="for(...) sum += i; // ✗ 反复拆箱再装箱" vertex="1">
          <mxGeometry height="18" width="260" x="520" y="202" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FECACA;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="75" width="370" x="475" y="235" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="2" vertex="1">
          <mxGeometry height="30" width="30" x="485" y="255" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#991B1B;fontStyle=1;spacingLeft=5;" value="用 == 比较包装类型" vertex="1">
          <mxGeometry height="22" width="200" x="520" y="240" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#991B1B;rounded=1;spacingLeft=8;" value="a == b  // ✗ 比较的是对象地址" vertex="1">
          <mxGeometry height="18" width="220" x="520" y="265" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=10;fontColor=#991B1B;spacingLeft=5;" value="(注:仅在缓存范围内 -128~127 有效,&lt;br&gt;超出范围则失效,极易产生 Bug)" vertex="1">
          <mxGeometry height="24" width="280" x="520" y="285" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FECACA;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="60" width="370" x="475" y="320" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#EF4444;strokeColor=#DC2626;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" value="3" vertex="1">
          <mxGeometry height="30" width="30" x="485" y="335" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontColor=#991B1B;fontStyle=1;spacingLeft=5;" value="局部变量不必要地使用包装类型" vertex="1">
          <mxGeometry height="22" width="250" x="520" y="325" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#991B1B;rounded=1;spacingLeft=8;" value="Integer i = 0;  // ✗ 增加了 GC 压力" vertex="1">
          <mxGeometry height="18" width="220" x="520" y="352" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#3B82F6;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="230" width="820" x="40" y="410" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📝 实体类 (POJO/Entity) 属性定义原则" vertex="1">
          <mxGeometry height="32" width="280" x="40" y="410" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BFDBFE;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="85" width="380" x="55" y="460" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="场景:数据库字段允许为 NULL" vertex="1">
          <mxGeometry height="24" width="180" x="70" y="470" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#3B82F6;" value="→" vertex="1">
          <mxGeometry height="28" width="30" x="250" y="470" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="必须使用包装类型" vertex="1">
          <mxGeometry height="24" width="120" x="290" y="470" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=#F0FDF4;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#166534;rounded=1;spacingLeft=8;" value="private Integer age; // 如果用 int 接收 null&lt;br&gt;// 会报 NullPointerException" vertex="1">
          <mxGeometry height="30" width="340" x="70" y="505" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BFDBFE;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="85" width="390" x="455" y="460" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#1E40AF;rounded=1;" value="场景:字段非空 (NOT NULL) 且有默认值" vertex="1">
          <mxGeometry height="24" width="220" x="470" y="470" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=24;fontStyle=1;fontColor=#3B82F6;" value="→" vertex="1">
          <mxGeometry height="28" width="30" x="690" y="470" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="可用基本类型&lt;div&gt;&amp;nbsp;(视团队规范)&lt;/div&gt;" vertex="1">
          <mxGeometry height="34" width="120" x="720" y="465" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="text;html=1;strokeColor=none;fillColor=#FFFBEB;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#92400E;rounded=1;spacingLeft=8;" value="private int status;  // 省略了 null 检查&lt;br&gt;// 默认值为 0" vertex="1">
          <mxGeometry height="30" width="360" x="470" y="505" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#8B5CF6;strokeWidth=2;arcSize=4;" value="" vertex="1">
          <mxGeometry height="65" width="790" x="55" y="560" as="geometry" />
        </mxCell>
        <mxCell id="52" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="📚 阿里巴巴 Java 开发手册建议" vertex="1">
          <mxGeometry height="26" width="200" x="55" y="560" as="geometry" />
        </mxCell>
        <mxCell id="53" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=13;fontStyle=1;fontColor=#6D28D9;spacingLeft=15;" value="【强制】所有的 POJO 类属性必须使用包装数据类型" vertex="1">
          <mxGeometry height="25" width="400" x="60" y="590" as="geometry" />
        </mxCell>
        <mxCell id="54" parent="1" style="text;html=1;strokeColor=none;fillColor=#F3E8FF;align=left;verticalAlign=middle;fontSize=12;fontColor=#6D28D9;rounded=1;spacingLeft=10;" value="原因:RPC 调用的返回值和数据库查询结果可能为 null" vertex="1">
          <mxGeometry height="25" width="380" x="450" y="590" as="geometry" />
        </mxCell>
        <mxCell id="55" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#F59E0B;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="140" width="820" x="40" y="660" as="geometry" />
        </mxCell>
        <mxCell id="56" parent="1" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="⚡ 性能对比 (Performance)" vertex="1">
          <mxGeometry height="28" width="200" x="40" y="660" as="geometry" />
        </mxCell>
        <mxCell id="57" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="80" width="370" x="55" y="705" as="geometry" />
        </mxCell>
        <mxCell id="58" parent="1" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="❌ 慢 (约 6000ms)" vertex="1">
          <mxGeometry height="22" width="120" x="65" y="715" as="geometry" />
        </mxCell>
        <mxCell id="59" parent="1" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#991B1B;rounded=1;spacingLeft=8;" value="Long sum = 0L;&lt;br&gt;for(long i=0; i &lt; Integer.MAX_VALUE; i++)&lt;br&gt;    sum += i; // 每次 += 都在创建新对象" vertex="1">
          <mxGeometry height="38" width="350" x="65" y="740" as="geometry" />
        </mxCell>
        <mxCell id="60" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#BBF7D0;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="80" width="400" x="445" y="705" as="geometry" />
        </mxCell>
        <mxCell id="61" parent="1" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="✅ 快 (约 600ms)" vertex="1">
          <mxGeometry height="22" width="120" x="455" y="715" as="geometry" />
        </mxCell>
        <mxCell id="62" parent="1" style="text;html=1;strokeColor=none;fillColor=#FFFFFF;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#166534;rounded=1;spacingLeft=8;" value="long sum = 0L; // 使用基本类型&lt;br&gt;for(long i=0; i &lt; Integer.MAX_VALUE; i++)&lt;br&gt;    sum += i; // 纯数值计算,无对象创建" vertex="1">
          <mxGeometry height="38" width="380" x="455" y="740" as="geometry" />
        </mxCell>
        <mxCell id="63" parent="1" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=18;fontStyle=1;fontColor=#FFFFFF;rounded=1;" value="10x" vertex="1">
          <mxGeometry height="26" width="50" x="590" y="670" as="geometry" />
        </mxCell>
        <mxCell id="64" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#8B5CF6;" value="Faster" vertex="1">
          <mxGeometry height="22" width="50" x="645" y="672" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

八、BigDecimal 精确计算

8.1 为什么需要 BigDecimal

public class WhyBigDecimalDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // 浮点数不适合金融计算
        // ═══════════════════════════════════════════════════════════════

        double price = 0.1;
        double quantity = 0.2;
        double total = price + quantity;

        System.out.println("0.1 + 0.2 = " + total);
        // 输出: 0.30000000000000004

        // 如果这是钱...
        double money = 1.0 - 0.9;
        System.out.println("1.0 - 0.9 = " + money);
        // 输出: 0.09999999999999998

        // 累计误差
        double sum = 0;
        for (int i = 0; i < 10; i++) {
            sum += 0.1;
        }
        System.out.println("0.1 × 10 = " + sum);
        // 输出: 0.9999999999999999
    }
}

8.2 BigDecimal 基本用法

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalBasicDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // 创建 BigDecimal(推荐使用 String 构造器)
        // ═══════════════════════════════════════════════════════════════

        // ✅ 推荐:String 构造器,精确
        BigDecimal a = new BigDecimal("0.1");
        BigDecimal b = new BigDecimal("0.2");

        // ❌ 不推荐:double 构造器,已经有精度问题
        BigDecimal bad = new BigDecimal(0.1);
        System.out.println("String构造: " + a);   // 0.1
        System.out.println("Double构造: " + bad); // 0.1000000000000000055511151231257827021181...

        // ✅ 或使用 valueOf(内部转为 String)
        BigDecimal c = BigDecimal.valueOf(0.1);

        // ═══════════════════════════════════════════════════════════════
        // 四则运算
        // ═══════════════════════════════════════════════════════════════

        BigDecimal x = new BigDecimal("10");
        BigDecimal y = new BigDecimal("3");

        System.out.println("加法: " + x.add(y));        // 13
        System.out.println("减法: " + x.subtract(y));   // 7
        System.out.println("乘法: " + x.multiply(y));   // 30

        // 除法必须指定精度,否则除不尽会抛异常
        // System.out.println(x.divide(y));  // ❌ ArithmeticException
        System.out.println("除法: " + x.divide(y, 2, RoundingMode.HALF_UP));  // 3.33

        // ═══════════════════════════════════════════════════════════════
        // 精度和舍入模式
        // ═══════════════════════════════════════════════════════════════

        BigDecimal value = new BigDecimal("3.1415926");

        // 保留 2 位小数,四舍五入
        BigDecimal rounded = value.setScale(2, RoundingMode.HALF_UP);
        System.out.println("四舍五入: " + rounded);  // 3.14

        // 向上取整
        System.out.println("向上取整: " + value.setScale(2, RoundingMode.UP));  // 3.15

        // 向下取整
        System.out.println("向下取整: " + value.setScale(2, RoundingMode.DOWN));  // 3.14

        // 银行家舍入(四舍六入五成双)
        System.out.println("银行家舍入: " + new BigDecimal("2.5").setScale(0, RoundingMode.HALF_EVEN));  // 2
        System.out.println("银行家舍入: " + new BigDecimal("3.5").setScale(0, RoundingMode.HALF_EVEN));  // 4
    }
}

8.3 舍入模式详解

 <mxfile>
  <diagram id="bigdecimal-rounding-modes" name="BigDecimal舍入模式">
    <mxGraphModel dx="900" dy="750" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="750">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" value="BigDecimal 舍入模式" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#1E293B;" vertex="1" parent="1">
          <mxGeometry x="250" y="15" width="400" height="40" as="geometry" />
        </mxCell>
        <mxCell id="3" value="RoundingMode 枚举详解" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=13;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="335" y="55" width="230" height="26" as="geometry" />
        </mxCell>
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F8FAFC;strokeColor=#E2E8F0;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="40" y="95" width="820" height="520" as="geometry" />
        </mxCell>
        <mxCell id="5" value="RoundingMode" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" vertex="1" parent="1">
          <mxGeometry x="55" y="110" width="150" height="38" as="geometry" />
        </mxCell>
        <mxCell id="6" value="说明" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" vertex="1" parent="1">
          <mxGeometry x="205" y="110" width="280" height="38" as="geometry" />
        </mxCell>
        <mxCell id="7" value="示例" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" vertex="1" parent="1">
          <mxGeometry x="485" y="110" width="200" height="38" as="geometry" />
        </mxCell>
        <mxCell id="8" value="方向" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#1E293B;strokeColor=#0F172A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#FFFFFF;" vertex="1" parent="1">
          <mxGeometry x="685" y="110" width="160" height="38" as="geometry" />
        </mxCell>
        <mxCell id="9" value="UP" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DBEAFE;strokeColor=#BFDBFE;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#1E40AF;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="55" y="148" width="150" height="50" as="geometry" />
        </mxCell>
        <mxCell id="10" value="远离零方向舍入&lt;br&gt;(绝对值变大)" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=12;fontColor=#1E40AF;" vertex="1" parent="1">
          <mxGeometry x="205" y="148" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="11" value="2.5 → 3&lt;br&gt;-2.5 → -3" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=12;fontFamily=Courier New;fontColor=#1E40AF;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="148" width="200" height="50" as="geometry" />
        </mxCell>
        <mxCell id="12" value="← 0 →" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#BFDBFE;strokeWidth=1;fontSize=18;fontStyle=1;fontColor=#3B82F6;" vertex="1" parent="1">
          <mxGeometry x="685" y="148" width="160" height="50" as="geometry" />
        </mxCell>
        <mxCell id="13" value="DOWN" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF3C7;strokeColor=#FDE68A;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#92400E;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="55" y="198" width="150" height="50" as="geometry" />
        </mxCell>
        <mxCell id="14" value="趋向零方向舍入&lt;br&gt;(截断小数部分)" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontColor=#92400E;" vertex="1" parent="1">
          <mxGeometry x="205" y="198" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="15" value="2.5 → 2&lt;br&gt;-2.5 → -2" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=12;fontFamily=Courier New;fontColor=#92400E;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="198" width="200" height="50" as="geometry" />
        </mxCell>
        <mxCell id="16" value="→ 0 ←" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#FDE68A;strokeWidth=1;fontSize=18;fontStyle=1;fontColor=#F59E0B;" vertex="1" parent="1">
          <mxGeometry x="685" y="198" width="160" height="50" as="geometry" />
        </mxCell>
        <mxCell id="17" value="CEILING" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#DCFCE7;strokeColor=#BBF7D0;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#166534;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="55" y="248" width="150" height="50" as="geometry" />
        </mxCell>
        <mxCell id="18" value="向正无穷方向舍入&lt;br&gt;(天花板)" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontColor=#166534;" vertex="1" parent="1">
          <mxGeometry x="205" y="248" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="19" value="2.5 → 3&lt;br&gt;-2.5 → -2" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#BBF7D0;strokeWidth=1;fontSize=12;fontFamily=Courier New;fontColor=#166534;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="248" width="200" height="50" as="geometry" />
        </mxCell>
        <mxCell id="20" value="→ +∞" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#BBF7D0;strokeWidth=1;fontSize=18;fontStyle=1;fontColor=#22C55E;" vertex="1" parent="1">
          <mxGeometry x="685" y="248" width="160" height="50" as="geometry" />
        </mxCell>
        <mxCell id="21" value="FLOOR" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#F3E8FF;strokeColor=#E9D5FF;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#6D28D9;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="55" y="298" width="150" height="50" as="geometry" />
        </mxCell>
        <mxCell id="22" value="向负无穷方向舍入&lt;br&gt;(地板)" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#E9D5FF;strokeWidth=1;fontSize=12;fontColor=#6D28D9;" vertex="1" parent="1">
          <mxGeometry x="205" y="298" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="23" value="2.5 → 2&lt;br&gt;-2.5 → -3" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#E9D5FF;strokeWidth=1;fontSize=12;fontFamily=Courier New;fontColor=#6D28D9;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="298" width="200" height="50" as="geometry" />
        </mxCell>
        <mxCell id="24" value="← -∞" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#E9D5FF;strokeWidth=1;fontSize=18;fontStyle=1;fontColor=#8B5CF6;" vertex="1" parent="1">
          <mxGeometry x="685" y="298" width="160" height="50" as="geometry" />
        </mxCell>
        <mxCell id="25" value="HALF_UP" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEE2E2;strokeColor=#FECACA;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#991B1B;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="55" y="348" width="150" height="50" as="geometry" />
        </mxCell>
        <mxCell id="26" value="四舍五入 ⭐&lt;br&gt;(最常用)" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#991B1B;" vertex="1" parent="1">
          <mxGeometry x="205" y="348" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="27" value="2.5 → 3&lt;br&gt;2.4 → 2" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=2;fontSize=12;fontFamily=Courier New;fontColor=#991B1B;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="348" width="200" height="50" as="geometry" />
        </mxCell>
        <mxCell id="28" value="≥0.5 进位" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#FECACA;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#EF4444;" vertex="1" parent="1">
          <mxGeometry x="685" y="348" width="160" height="50" as="geometry" />
        </mxCell>
        <mxCell id="29" value="HALF_DOWN" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#E0E7FF;strokeColor=#C7D2FE;strokeWidth=1;fontSize=14;fontStyle=1;fontColor=#3730A3;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="55" y="398" width="150" height="50" as="geometry" />
        </mxCell>
        <mxCell id="30" value="五舍六入&lt;br&gt;(0.5 舍去)" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EEF2FF;strokeColor=#C7D2FE;strokeWidth=1;fontSize=12;fontColor=#3730A3;" vertex="1" parent="1">
          <mxGeometry x="205" y="398" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="31" value="2.5 → 2&lt;br&gt;2.6 → 3" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EEF2FF;strokeColor=#C7D2FE;strokeWidth=1;fontSize=12;fontFamily=Courier New;fontColor=#3730A3;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="398" width="200" height="50" as="geometry" />
        </mxCell>
        <mxCell id="32" value="&gt;0.5 进位" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#EEF2FF;strokeColor=#C7D2FE;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#6366F1;" vertex="1" parent="1">
          <mxGeometry x="685" y="398" width="160" height="50" as="geometry" />
        </mxCell>
        <mxCell id="33" value="HALF_EVEN" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#D1FAE5;strokeColor=#6EE7B7;strokeWidth=2;fontSize=14;fontStyle=1;fontColor=#065F46;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="55" y="448" width="150" height="50" as="geometry" />
        </mxCell>
        <mxCell id="34" value="银行家舍入 💰&lt;br&gt;(四舍六入五成双)" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ECFDF5;strokeColor=#6EE7B7;strokeWidth=2;fontSize=12;fontStyle=1;fontColor=#065F46;" vertex="1" parent="1">
          <mxGeometry x="205" y="448" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="35" value="2.5 → 2&lt;br&gt;3.5 → 4" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ECFDF5;strokeColor=#6EE7B7;strokeWidth=2;fontSize=12;fontFamily=Courier New;fontColor=#065F46;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="448" width="200" height="50" as="geometry" />
        </mxCell>
        <mxCell id="36" value="0.5 向偶数" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ECFDF5;strokeColor=#6EE7B7;strokeWidth=2;fontSize=13;fontStyle=1;fontColor=#10B981;" vertex="1" parent="1">
          <mxGeometry x="685" y="448" width="160" height="50" as="geometry" />
        </mxCell>
        <mxCell id="37" value="UNNECESSARY" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEF9C3;strokeColor=#FDE047;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#854D0E;fontFamily=Courier New;" vertex="1" parent="1">
          <mxGeometry x="55" y="498" width="150" height="50" as="geometry" />
        </mxCell>
        <mxCell id="38" value="断言精确结果&lt;br&gt;否则抛 ArithmeticException" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEFCE8;strokeColor=#FDE047;strokeWidth=1;fontSize=12;fontColor=#854D0E;" vertex="1" parent="1">
          <mxGeometry x="205" y="498" width="280" height="50" as="geometry" />
        </mxCell>
        <mxCell id="39" value="1.0 → 1 ✓&lt;br&gt;1.5 → 异常 ✗" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEFCE8;strokeColor=#FDE047;strokeWidth=1;fontSize=12;fontFamily=Courier New;fontColor=#854D0E;fontStyle=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="498" width="200" height="50" as="geometry" />
        </mxCell>
        <mxCell id="40" value="必须精确" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#FEFCE8;strokeColor=#FDE047;strokeWidth=1;fontSize=13;fontStyle=1;fontColor=#EAB308;" vertex="1" parent="1">
          <mxGeometry x="685" y="498" width="160" height="50" as="geometry" />
        </mxCell>
        <mxCell id="41" value="常用" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="170" y="353" width="32" height="18" as="geometry" />
        </mxCell>
        <mxCell id="42" value="推荐" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="170" y="453" width="32" height="18" as="geometry" />
        </mxCell>
        <mxCell id="43" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ECFDF5;strokeColor=#10B981;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="40" y="630" width="820" height="100" as="geometry" />
        </mxCell>
        <mxCell id="44" value="💡 金融计算推荐" style="text;html=1;strokeColor=none;fillColor=#10B981;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="645" width="140" height="28" as="geometry" />
        </mxCell>
        <mxCell id="45" value="HALF_EVEN(银行家舍入)— 统计学上最公平,避免系统性偏差" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#065F46;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="210" y="645" width="500" height="28" as="geometry" />
        </mxCell>
        <mxCell id="46" value="原理:当小数部分恰好为 0.5 时,向最近的偶数舍入,长期累计误差趋近于零" style="text;html=1;strokeColor=none;fillColor=#D1FAE5;align=left;verticalAlign=middle;fontSize=12;fontColor=#065F46;rounded=1;spacingLeft=10;" vertex="1" parent="1">
          <mxGeometry x="60" y="685" width="520" height="28" as="geometry" />
        </mxCell>
        <mxCell id="47" value="bd.setScale(2, RoundingMode.HALF_EVEN)" style="text;html=1;strokeColor=none;fillColor=#065F46;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="590" y="688" width="255" height="24" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

8.4 BigDecimal 比较

public class BigDecimalCompareDemo {
    public static void main(String[] args) {
        BigDecimal a = new BigDecimal("1.0");
        BigDecimal b = new BigDecimal("1.00");
        
        // ═══════════════════════════════════════════════════════════════
        // equals() 比较值和精度(标度)
        // ═══════════════════════════════════════════════════════════════
        
        System.out.println("a.equals(b): " + a.equals(b));  // false!
        // 因为 1.0 的标度是 1,1.00 的标度是 2
        
        // ═══════════════════════════════════════════════════════════════
        // compareTo() 只比较数值大小(推荐)
        // ═══════════════════════════════════════════════════════════════
        
        System.out.println("a.compareTo(b): " + a.compareTo(b));  // 0
        // 0 表示相等,正数表示 a > b,负数表示 a < b
        
        // 正确的相等判断
        if (a.compareTo(b) == 0) {
            System.out.println("a 和 b 数值相等");
        }
        
        // ═══════════════════════════════════════════════════════════════
        // 使用 stripTrailingZeros() 去除尾部零后比较
        // ═══════════════════════════════════════════════════════════════
        
        BigDecimal c = new BigDecimal("1.0").stripTrailingZeros();
        BigDecimal d = new BigDecimal("1.00").stripTrailingZeros();
        System.out.println("stripTrailingZeros后 equals: " + c.equals(d));  // true
    }
}
<mxfile>
  <diagram id="bigdecimal-compare" name="BigDecimal equals vs compareTo">
    <mxGraphModel dx="926" dy="817" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="720" 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=#fafafa;strokeColor=#7b1fa2;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="730" width="820" x="40" y="20" 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=26;fontStyle=1;fontColor=#4a148c;" value="BigDecimal equals vs compareTo" vertex="1">
          <mxGeometry height="45" width="500" x="200" y="35" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=#9c27b0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="205" width="780" x="60" y="95" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#9c27b0;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="40" width="780" x="60" y="95" 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=17;fontStyle=1;fontColor=#ffffff;" value="🔍 BigDecimal 的内部表示" vertex="1">
          <mxGeometry height="40" width="780" x="60" y="95" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#ce93d8;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="45" width="720" x="90" y="150" 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=16;" value="&lt;font face=&quot;Courier New&quot; style=&quot;font-size: 16px;&quot;&gt;BigDecimal = &lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;unscaledValue&lt;/b&gt;&lt;/font&gt; × 10&lt;sup&gt;(&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;-scale&lt;/b&gt;&lt;/font&gt;)&lt;/sup&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="45" width="720" x="90" y="150" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="350" x="90" y="210" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;spacingLeft=15;" value="&lt;div style=&quot;text-align: left;&quot;&gt;&lt;font face=&quot;Courier New&quot;&gt;&lt;b style=&quot;font-size: 15px;&quot;&gt;1.0&lt;/b&gt;&lt;font style=&quot;font-size: 14px;&quot;&gt; = 10 × 10&lt;sup&gt;-1&lt;/sup&gt;&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;font color=&quot;#1565c0&quot;&gt;unscaledValue = &lt;b&gt;10&lt;/b&gt;&lt;/font&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;font color=&quot;#c62828&quot;&gt;scale = &lt;b&gt;1&lt;/b&gt;&lt;/font&gt;&lt;/div&gt;" vertex="1">
          <mxGeometry height="70" width="350" x="90" y="210" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="350" x="460" y="210" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;spacingLeft=15;" value="&lt;div style=&quot;text-align: left;&quot;&gt;&lt;font face=&quot;Courier New&quot;&gt;&lt;b style=&quot;font-size: 15px;&quot;&gt;1.00&lt;/b&gt;&lt;font style=&quot;font-size: 14px;&quot;&gt; = 100 × 10&lt;sup&gt;-2&lt;/sup&gt;&lt;/font&gt;&lt;/font&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;font color=&quot;#1565c0&quot;&gt;unscaledValue = &lt;b&gt;100&lt;/b&gt;&lt;/font&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;font color=&quot;#c62828&quot;&gt;scale = &lt;b&gt;2&lt;/b&gt;&lt;/font&gt;&lt;/div&gt;" vertex="1">
          <mxGeometry height="70" width="350" x="460" y="210" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=24;fontStyle=1;fontColor=#e53935;" value="≠" vertex="1">
          <mxGeometry height="40" width="40" x="430" y="225" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;fontColor=#7b1fa2;fontStyle=1;" value="⚠️ 两者数值相等,但内部表示不同!" vertex="1">
          <mxGeometry height="20" width="340" x="280" y="280" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffebee;strokeColor=#e53935;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="200" width="380" x="60" y="310" as="geometry" />
        </mxCell>
        <mxCell id="16" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e53935;strokeColor=none;arcSize=8;" value="" vertex="1">
          <mxGeometry height="45" width="380" x="60" y="310" 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=17;fontStyle=1;fontColor=#ffffff;" value="❌ equals() 方法" vertex="1">
          <mxGeometry height="45" width="380" x="60" y="310" as="geometry" />
        </mxCell>
        <mxCell id="18" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#ef9a9a;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="55" width="340" x="80" y="365" 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;" value="比较 &lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;unscaledValue&lt;/b&gt;&lt;/font&gt;&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;scale&lt;/b&gt;&lt;/font&gt; 都相等&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 12px;&quot;&gt;精确匹配内部表示&lt;/font&gt;" vertex="1">
          <mxGeometry height="55" width="340" x="80" y="365" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffcdd2;strokeColor=#e53935;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="65" width="340" x="80" y="430" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font face=&quot;Courier New&quot;&gt;new BigDecimal(&quot;1.0&quot;)&lt;br&gt;  .equals(new BigDecimal(&quot;1.00&quot;))&lt;/font&gt;&lt;br&gt;&lt;font style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;&lt;font color=&quot;#c62828&quot;&gt;false&lt;/font&gt;&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="65" width="340" x="80" y="430" as="geometry" />
        </mxCell>
        <mxCell id="22" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="200" width="380" x="460" y="310" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;arcSize=8;" value="" vertex="1">
          <mxGeometry height="45" width="380" x="460" y="310" 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=17;fontStyle=1;fontColor=#ffffff;" value="✅ compareTo() 方法" vertex="1">
          <mxGeometry height="45" width="380" x="460" y="310" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="55" width="340" x="480" y="365" 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=14;" value="只比较 &lt;b&gt;数学值&lt;/b&gt;,忽略 scale&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 12px;&quot;&gt;语义上的数值比较&lt;/font&gt;" vertex="1">
          <mxGeometry height="55" width="340" x="480" y="365" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#c8e6c9;strokeColor=#4caf50;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="65" width="340" x="480" y="430" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font face=&quot;Courier New&quot;&gt;new BigDecimal(&quot;1.0&quot;)&lt;br&gt;  .compareTo(new BigDecimal(&quot;1.00&quot;))&lt;/font&gt;&lt;br&gt;&lt;font style=&quot;font-size: 15px;&quot;&gt;&lt;b&gt;&lt;font color=&quot;#2e7d32&quot;&gt;0&lt;/font&gt;&lt;/b&gt; (相等)&lt;/font&gt;" vertex="1">
          <mxGeometry height="65" width="340" x="480" y="430" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="ellipse;whiteSpace=wrap;html=1;fillColor=#7b1fa2;strokeColor=none;shadow=1;" value="" vertex="1">
          <mxGeometry height="50" width="50" x="425" y="385" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;VS&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="50" width="50" x="425" y="385" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8eaf6;strokeColor=#5c6bc0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="120" width="780" x="60" y="525" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#5c6bc0;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="200" x="60" y="525" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;比较场景&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="200" x="60" y="525" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e53935;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="290" x="260" y="525" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;equals()&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="290" x="260" y="525" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="290" x="550" y="525" as="geometry" />
        </mxCell>
        <mxCell id="37" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;compareTo() == 0&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="290" x="550" y="525" as="geometry" />
        </mxCell>
        <mxCell id="38" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#c5cae9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="200" x="60" y="560" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font face=&quot;Courier New&quot;&gt;1.0 vs 1.00&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="200" x="60" y="560" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffebee;strokeColor=#ef9a9a;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="290" x="260" y="560" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;false&lt;/b&gt;&lt;/font&gt; &lt;font color=&quot;#757575&quot;&gt;(scale不同)&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="290" x="260" y="560" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="290" x="550" y="560" as="geometry" />
        </mxCell>
        <mxCell id="43" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;true&lt;/b&gt;&lt;/font&gt; &lt;font color=&quot;#757575&quot;&gt;(数值相等)&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="290" x="550" y="560" as="geometry" />
        </mxCell>
        <mxCell id="44" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#c5cae9;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="200" x="60" y="600" as="geometry" />
        </mxCell>
        <mxCell id="45" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font face=&quot;Courier New&quot;&gt;1.0 vs 1.0&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="200" x="60" y="600" as="geometry" />
        </mxCell>
        <mxCell id="46" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="290" x="260" y="600" as="geometry" />
        </mxCell>
        <mxCell id="47" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;true&lt;/b&gt;&lt;/font&gt; &lt;font color=&quot;#757575&quot;&gt;(完全相同)&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="290" x="260" y="600" as="geometry" />
        </mxCell>
        <mxCell id="48" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#a5d6a7;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="40" width="290" x="550" y="600" as="geometry" />
        </mxCell>
        <mxCell id="49" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font color=&quot;#2e7d32&quot;&gt;&lt;b&gt;true&lt;/b&gt;&lt;/font&gt; &lt;font color=&quot;#757575&quot;&gt;(数值相等)&lt;/font&gt;" vertex="1">
          <mxGeometry height="40" width="290" x="550" y="600" as="geometry" />
        </mxCell>
        <mxCell id="50" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff8e1;strokeColor=#ffa000;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="35" width="780" x="60" y="660" as="geometry" />
        </mxCell>
        <mxCell id="51" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#e65100;" value="💡 &lt;b&gt;最佳实践&lt;/b&gt;:比较 BigDecimal 数值时,始终使用 &lt;font face=&quot;Courier New&quot; color=&quot;#2e7d32&quot;&gt;&lt;b&gt;compareTo() == 0&lt;/b&gt;&lt;/font&gt; 而非 equals()" vertex="1">
          <mxGeometry height="35" width="780" x="60" y="660" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

8.5 工具类封装

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * BigDecimal 计算工具类
 */
public final class BigDecimalUtil {
    
    // 默认精度
    private static final int DEFAULT_SCALE = 2;
    // 默认舍入模式
    private static final RoundingMode DEFAULT_ROUNDING = RoundingMode.HALF_UP;
    
    private BigDecimalUtil() {}
    
    /**
     * 加法
     */
    public static BigDecimal add(double v1, double v2) {
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.add(b2);
    }
    
    /**
     * 减法
     */
    public static BigDecimal subtract(double v1, double v2) {
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.subtract(b2);
    }
    
    /**
     * 乘法
     */
    public static BigDecimal multiply(double v1, double v2) {
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.multiply(b2);
    }
    
    /**
     * 除法(使用默认精度和舍入模式)
     */
    public static BigDecimal divide(double v1, double v2) {
        return divide(v1, v2, DEFAULT_SCALE, DEFAULT_ROUNDING);
    }
    
    /**
     * 除法(指定精度和舍入模式)
     */
    public static BigDecimal divide(double v1, double v2, int scale, RoundingMode mode) {
        if (scale < 0) {
            throw new IllegalArgumentException("精度不能为负数");
        }
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.divide(b2, scale, mode);
    }
    
    /**
     * 四舍五入
     */
    public static BigDecimal round(double v, int scale) {
        return BigDecimal.valueOf(v).setScale(scale, DEFAULT_ROUNDING);
    }
    
    /**
     * 比较大小
     * @return 正数: v1 > v2, 0: v1 == v2, 负数: v1 < v2
     */
    public static int compare(double v1, double v2) {
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.compareTo(b2);
    }
}

九、BigInteger 大整数

9.1 为什么需要 BigInteger

public class WhyBigIntegerDemo {
    public static void main(String[] args) {
        // long 的最大值
        long maxLong = Long.MAX_VALUE;  // 9223372036854775807
        System.out.println("Long.MAX_VALUE = " + maxLong);
        
        // 溢出
        System.out.println("Long.MAX_VALUE + 1 = " + (maxLong + 1));  // 负数!
        
        // BigInteger 可以表示任意大小的整数
        BigInteger big = new BigInteger("9223372036854775808");  // Long.MAX_VALUE + 1
        System.out.println("BigInteger = " + big);
        
        // 更大的数
        BigInteger huge = new BigInteger("123456789012345678901234567890");
        System.out.println("Huge = " + huge);
    }
}

9.2 BigInteger 基本用法

import java.math.BigInteger;

public class BigIntegerDemo {
    public static void main(String[] args) {
        // ═══════════════════════════════════════════════════════════════
        // 创建 BigInteger
        // ═══════════════════════════════════════════════════════════════
        
        BigInteger a = new BigInteger("12345678901234567890");
        BigInteger b = BigInteger.valueOf(100L);  // 从 long 创建
        BigInteger c = BigInteger.TEN;            // 常量
        BigInteger d = BigInteger.ZERO;
        BigInteger e = BigInteger.ONE;
        
        // ═══════════════════════════════════════════════════════════════
        // 四则运算
        // ═══════════════════════════════════════════════════════════════
        
        BigInteger x = new BigInteger("1000000000000");
        BigInteger y = new BigInteger("999999999999");
        
        System.out.println("加法: " + x.add(y));
        System.out.println("减法: " + x.subtract(y));
        System.out.println("乘法: " + x.multiply(y));
        System.out.println("除法: " + x.divide(y));
        System.out.println("取余: " + x.remainder(y));
        System.out.println("幂运算: " + x.pow(2));
        
        // 同时获取商和余数
        BigInteger[] divideAndRemainder = x.divideAndRemainder(y);
        System.out.println("商: " + divideAndRemainder[0]);
        System.out.println("余数: " + divideAndRemainder[1]);
        
        // ═══════════════════════════════════════════════════════════════
        // 位运算
        // ═══════════════════════════════════════════════════════════════
        
        BigInteger num = new BigInteger("100");
        System.out.println("左移2位: " + num.shiftLeft(2));   // 400
        System.out.println("右移2位: " + num.shiftRight(2));  // 25
        System.out.println("按位与: " + num.and(new BigInteger("50")));
        System.out.println("按位或: " + num.or(new BigInteger("50")));
        
        // ═══════════════════════════════════════════════════════════════
        // 比较
        // ═══════════════════════════════════════════════════════════════
        
        System.out.println("compareTo: " + x.compareTo(y));  // 正数表示 x > y
        System.out.println("equals: " + x.equals(y));
        
        // ═══════════════════════════════════════════════════════════════
        // 类型转换
        // ═══════════════════════════════════════════════════════════════
        
        BigInteger small = new BigInteger("100");
        int intValue = small.intValue();      // 转为 int(可能溢出)
        long longValue = small.longValue();   // 转为 long(可能溢出)
        
        // 安全转换(溢出时抛出 ArithmeticException)
        int safeInt = small.intValueExact();
        long safeLong = small.longValueExact();
    }
}

9.3 BigInteger 内部原理

<mxfile>
  <diagram id="biginteger-internal" name="BigInteger 内部实现">
    <mxGraphModel dx="926" dy="817" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="700" 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=#fafafa;strokeColor=#1565c0;strokeWidth=3;shadow=1;" value="" vertex="1">
          <mxGeometry height="700" width="820" x="40" y="20" 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=26;fontStyle=1;fontColor=#0d47a1;" value="BigInteger 内部实现" vertex="1">
          <mxGeometry height="45" width="340" x="280" y="35" as="geometry" />
        </mxCell>
        <mxCell id="4" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e3f2fd;strokeColor=#1976d2;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="180" width="780" x="60" y="95" as="geometry" />
        </mxCell>
        <mxCell id="5" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1976d2;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="40" width="780" x="60" y="95" 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=17;fontStyle=1;fontColor=#ffffff;" value="📦 BigInteger 类结构" vertex="1">
          <mxGeometry height="40" width="780" x="60" y="95" as="geometry" />
        </mxCell>
        <mxCell id="7" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#424242;" value="BigInteger 内部使用 &lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;int[] 数组&lt;/b&gt;&lt;/font&gt; 存储数值,可表示任意精度的整数" vertex="1">
          <mxGeometry height="25" width="780" x="60" y="140" as="geometry" />
        </mxCell>
        <mxCell id="8" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#263238;strokeColor=#37474f;strokeWidth=2;shadow=1;" value="" vertex="1">
          <mxGeometry height="90" width="720" x="90" y="170" as="geometry" />
        </mxCell>
        <mxCell id="9" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;spacingLeft=15;" value="&lt;div style=&quot;text-align: left; line-height: 1.6;&quot;&gt;&lt;font face=&quot;Courier New&quot; color=&quot;#e0e0e0&quot;&gt;&lt;font color=&quot;#ce93d8&quot;&gt;public class&lt;/font&gt; &lt;font color=&quot;#64b5f6&quot;&gt;BigInteger&lt;/font&gt; &lt;font color=&quot;#ce93d8&quot;&gt;extends&lt;/font&gt; Number &lt;font color=&quot;#ce93d8&quot;&gt;implements&lt;/font&gt; Comparable&amp;lt;BigInteger&amp;gt; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=&quot;#ce93d8&quot;&gt;final int&lt;/font&gt; &lt;font color=&quot;#ffcc80&quot;&gt;signum&lt;/font&gt;;    &lt;font color=&quot;#81c784&quot;&gt;// 符号:-1, 0, 1&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=&quot;#ce93d8&quot;&gt;final int[]&lt;/font&gt; &lt;font color=&quot;#ffcc80&quot;&gt;mag&lt;/font&gt;;     &lt;font color=&quot;#81c784&quot;&gt;// 数值的绝对值(大端序)&lt;/font&gt;&lt;br&gt;}&lt;/font&gt;&lt;/div&gt;" vertex="1">
          <mxGeometry height="90" width="720" x="90" y="170" as="geometry" />
        </mxCell>
        <mxCell id="10" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff3e0;strokeColor=#ff9800;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="120" width="380" x="60" y="290" as="geometry" />
        </mxCell>
        <mxCell id="11" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ff9800;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="380" x="60" y="290" as="geometry" />
        </mxCell>
        <mxCell id="12" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=15;fontStyle=0;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;signum&lt;/b&gt; - 符号位&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="380" x="60" y="290" as="geometry" />
        </mxCell>
        <mxCell id="13" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe0b2;strokeColor=#ffb74d;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="100" x="80" y="335" as="geometry" />
        </mxCell>
        <mxCell id="14" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;-1&lt;/b&gt;&lt;/font&gt; 负数" vertex="1">
          <mxGeometry height="30" width="100" x="80" y="335" as="geometry" />
        </mxCell>
        <mxCell id="15" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe0b2;strokeColor=#ffb74d;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="100" x="190" y="335" 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=13;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;0&lt;/b&gt;&lt;/font&gt; 零" vertex="1">
          <mxGeometry height="30" width="100" x="190" y="335" as="geometry" />
        </mxCell>
        <mxCell id="17" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe0b2;strokeColor=#ffb74d;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="30" width="120" x="300" y="335" 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=13;" value="&lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;1&lt;/b&gt;&lt;/font&gt; 正数" vertex="1">
          <mxGeometry height="30" width="120" x="300" y="335" 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=12;" value="&lt;font color=&quot;#757575&quot;&gt;仅存储符号,不参与数值计算&lt;/font&gt;" vertex="1">
          <mxGeometry height="25" width="380" x="60" y="375" as="geometry" />
        </mxCell>
        <mxCell id="20" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e8f5e9;strokeColor=#4caf50;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="120" width="380" x="460" y="290" as="geometry" />
        </mxCell>
        <mxCell id="21" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#4caf50;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="35" width="380" x="460" y="290" 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=15;" value="&lt;font color=&quot;#ffffff&quot;&gt;&lt;b&gt;mag[]&lt;/b&gt; - 数值数组&lt;/font&gt;" vertex="1">
          <mxGeometry height="35" width="380" x="460" y="290" as="geometry" />
        </mxCell>
        <mxCell id="23" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;spacingLeft=20;" value="&lt;div style=&quot;text-align: left;&quot;&gt;• 存储数值的&lt;b&gt;绝对值&lt;/b&gt;&lt;br&gt;• 采用&lt;font color=&quot;#1565c0&quot;&gt;&lt;b&gt;大端序&lt;/b&gt;&lt;/font&gt;(高位在前)&lt;br&gt;• 每个 int 存储 &lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;32 位&lt;/b&gt;&lt;/font&gt;数据&lt;br&gt;• 数组长度动态扩展&lt;/div&gt;" vertex="1">
          <mxGeometry height="75" width="380" x="460" y="330" as="geometry" />
        </mxCell>
        <mxCell id="24" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=#9c27b0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="200" width="780" x="60" y="425" as="geometry" />
        </mxCell>
        <mxCell id="25" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#9c27b0;strokeColor=none;" value="" vertex="1">
          <mxGeometry height="40" width="780" x="60" y="425" 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=16;fontStyle=1;fontColor=#ffffff;" value="📝 示例:BigInteger(&quot;12345678901234567890&quot;)" vertex="1">
          <mxGeometry height="40" width="780" x="60" y="425" as="geometry" />
        </mxCell>
        <mxCell id="27" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#ce93d8;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="55" width="200" x="90" y="480" as="geometry" />
        </mxCell>
        <mxCell id="28" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;" value="&lt;font face=&quot;Courier New&quot;&gt;signum = &lt;font color=&quot;#4caf50&quot;&gt;&lt;b&gt;1&lt;/b&gt;&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 12px;&quot;&gt;(正数)&lt;/font&gt;" vertex="1">
          <mxGeometry height="55" width="200" x="90" y="480" as="geometry" />
        </mxCell>
        <mxCell id="29" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#ce93d8;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="130" width="510" x="310" y="480" as="geometry" />
        </mxCell>
        <mxCell id="30" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;spacingLeft=10;" value="&lt;font face=&quot;Courier New&quot;&gt;mag[] = &lt;/font&gt;" vertex="1">
          <mxGeometry height="30" width="100" x="310" y="485" as="geometry" />
        </mxCell>
        <mxCell id="31" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e1bee7;strokeColor=#9c27b0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="150" x="330" y="530" as="geometry" />
        </mxCell>
        <mxCell id="32" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#6a1b9a&quot;&gt;&lt;b&gt;0xAB54A98C&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 11px;&quot;&gt;mag[0] - 高位&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#9e9e9e&quot; style=&quot;font-size: 10px;&quot;&gt;32 bits&lt;/font&gt;" vertex="1">
          <mxGeometry height="70" width="150" x="330" y="530" as="geometry" />
        </mxCell>
        <mxCell id="33" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#e1bee7;strokeColor=#9c27b0;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="70" width="150" x="490" y="530" as="geometry" />
        </mxCell>
        <mxCell id="34" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=13;" value="&lt;font face=&quot;Courier New&quot; color=&quot;#6a1b9a&quot;&gt;&lt;b&gt;0xEB1F0AD2&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#757575&quot; style=&quot;font-size: 11px;&quot;&gt;mag[1] - 低位&lt;/font&gt;&lt;br&gt;&lt;font color=&quot;#9e9e9e&quot; style=&quot;font-size: 10px;&quot;&gt;32 bits&lt;/font&gt;" vertex="1">
          <mxGeometry height="70" width="150" x="490" y="530" as="geometry" />
        </mxCell>
        <mxCell id="35" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#f3e5f5;strokeColor=#ce93d8;strokeWidth=1;strokeDasharray=3 3;" value="" vertex="1">
          <mxGeometry height="70" width="60" x="650" y="530" as="geometry" />
        </mxCell>
        <mxCell id="36" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;" value="&lt;font color=&quot;#9e9e9e&quot;&gt;...&lt;/font&gt;" vertex="1">
          <mxGeometry height="70" width="60" x="644" y="530" as="geometry" />
        </mxCell>
        <mxCell id="37" edge="1" parent="1" style="endArrow=classic;html=1;strokeColor=#9c27b0;strokeWidth=2;" value="">
          <mxGeometry height="50" relative="1" width="50" as="geometry">
            <mxPoint x="410" y="510" as="sourcePoint" />
            <mxPoint x="570" y="510" as="targetPoint" />
          </mxGeometry>
        </mxCell>
        <mxCell id="38" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=11;" value="&lt;font color=&quot;#9c27b0&quot; style=&quot;font-size: 11px;&quot;&gt;大端序&lt;/font&gt;" vertex="1">
          <mxGeometry height="20" width="60" x="460" y="490" as="geometry" />
        </mxCell>
        <mxCell id="39" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ede7f6;strokeColor=#7e57c2;strokeWidth=1;" value="" vertex="1">
          <mxGeometry height="70" width="100" x="715" y="530" as="geometry" />
        </mxCell>
        <mxCell id="40" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=12;fontColor=#5e35b1;" value="&lt;font style=&quot;font-size: 11px;&quot;&gt;多个 int&lt;br&gt;组合表示&lt;br&gt;&lt;b&gt;任意大整数&lt;/b&gt;&lt;/font&gt;" vertex="1">
          <mxGeometry height="70" width="100" x="715" y="530" as="geometry" />
        </mxCell>
        <mxCell id="41" parent="1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff8e1;strokeColor=#ffa000;strokeWidth=2;" value="" vertex="1">
          <mxGeometry height="35" width="780" x="60" y="640" as="geometry" />
        </mxCell>
        <mxCell id="42" parent="1" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=14;fontColor=#e65100;" value="⚠️ &lt;b&gt;注意&lt;/b&gt;:BigInteger 是&lt;font color=&quot;#c62828&quot;&gt;&lt;b&gt;不可变对象&lt;/b&gt;&lt;/font&gt;,每次运算(add、multiply等)都会创建新对象,注意性能开销" vertex="1">
          <mxGeometry height="35" width="780" x="60" y="640" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>

十、总结

<mxfile>
  <diagram id="data-types-overview" name="数据类型详解">
    <mxGraphModel dx="900" dy="780" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="900" pageHeight="780">
      <root>
        <mxCell id="0" />
        <mxCell id="1" parent="0" />
        <mxCell id="2" value="数据类型详解 - 核心要点" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;fontSize=26;fontStyle=1;fontColor=#1E293B;" vertex="1" parent="1">
          <mxGeometry x="225" y="15" width="450" height="40" as="geometry" />
        </mxCell>
        <mxCell id="3" value="Java 数据类型知识体系速览" style="text;html=1;strokeColor=none;fillColor=#6366F1;align=center;verticalAlign=middle;fontSize=12;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="325" y="55" width="250" height="24" as="geometry" />
        </mxCell>
        <mxCell id="4" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#EFF6FF;strokeColor=#3B82F6;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="40" y="95" width="400" height="165" as="geometry" />
        </mxCell>
        <mxCell id="5" value="📊 基本类型" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="55" y="108" width="120" height="30" as="geometry" />
        </mxCell>
        <mxCell id="6" value="Primitive Types" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#3B82F6;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="180" y="112" width="100" height="22" as="geometry" />
        </mxCell>
        <mxCell id="7" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BFDBFE;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="55" y="145" width="370" height="100" as="geometry" />
        </mxCell>
        <mxCell id="8" value="8 种" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="152" width="40" height="20" as="geometry" />
        </mxCell>
        <mxCell id="9" value="byte / short / int / long / float / double / char / boolean" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=left;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontColor=#1E40AF;rounded=1;spacingLeft=6;" vertex="1" parent="1">
          <mxGeometry x="110" y="150" width="305" height="24" as="geometry" />
        </mxCell>
        <mxCell id="10" value="特点" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="180" width="40" height="20" as="geometry" />
        </mxCell>
        <mxCell id="11" value="直接存储数值,效率高,存储在栈(局部)或堆(成员)中" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#1E40AF;spacingLeft=6;" vertex="1" parent="1">
          <mxGeometry x="110" y="178" width="305" height="24" as="geometry" />
        </mxCell>
        <mxCell id="12" value="默认值" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="208" width="50" height="20" as="geometry" />
        </mxCell>
        <mxCell id="13" value="0" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontStyle=1;fontColor=#1E40AF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="125" y="208" width="40" height="20" as="geometry" />
        </mxCell>
        <mxCell id="14" value="0.0" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontStyle=1;fontColor=#1E40AF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="175" y="208" width="40" height="20" as="geometry" />
        </mxCell>
        <mxCell id="15" value="false" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontStyle=1;fontColor=#1E40AF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="225" y="208" width="45" height="20" as="geometry" />
        </mxCell>
        <mxCell id="16" value="\u0000" style="text;html=1;strokeColor=none;fillColor=#DBEAFE;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontStyle=1;fontColor=#1E40AF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="280" y="208" width="55" height="20" as="geometry" />
        </mxCell>
        <mxCell id="17" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="460" y="95" width="400" height="165" as="geometry" />
        </mxCell>
        <mxCell id="18" value="📦 包装类型" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="475" y="108" width="120" height="30" as="geometry" />
        </mxCell>
        <mxCell id="19" value="Wrapper Classes" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#22C55E;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="600" y="112" width="110" height="22" as="geometry" />
        </mxCell>
        <mxCell id="20" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#BBF7D0;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="475" y="145" width="370" height="100" as="geometry" />
        </mxCell>
        <mxCell id="21" value="用途" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="152" width="40" height="20" as="geometry" />
        </mxCell>
        <mxCell id="22" value="泛型、集合 (List&lt;Integer&gt;)、可为 null 的场景" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#166534;spacingLeft=6;" vertex="1" parent="1">
          <mxGeometry x="530" y="150" width="305" height="24" as="geometry" />
        </mxCell>
        <mxCell id="23" value="机制" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="180" width="40" height="20" as="geometry" />
        </mxCell>
        <mxCell id="24" value="自动装箱 (valueOf) / 自动拆箱 (intValue)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#166534;spacingLeft=6;" vertex="1" parent="1">
          <mxGeometry x="530" y="178" width="305" height="24" as="geometry" />
        </mxCell>
        <mxCell id="25" value="缓存" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="208" width="40" height="20" as="geometry" />
        </mxCell>
        <mxCell id="26" value="Integer 默认缓存" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#166534;spacingLeft=6;" vertex="1" parent="1">
          <mxGeometry x="530" y="206" width="115" height="24" as="geometry" />
        </mxCell>
        <mxCell id="27" value="[-128, 127]" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontStyle=1;fontColor=#166534;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="645" y="208" width="80" height="20" as="geometry" />
        </mxCell>
        <mxCell id="28" value="此范围 == 有效" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#166534;" vertex="1" parent="1">
          <mxGeometry x="730" y="206" width="100" height="24" as="geometry" />
        </mxCell>
        <mxCell id="29" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FEF2F2;strokeColor=#EF4444;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="40" y="280" width="400" height="175" as="geometry" />
        </mxCell>
        <mxCell id="30" value="⚠️ 重点避坑" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="55" y="293" width="120" height="30" as="geometry" />
        </mxCell>
        <mxCell id="31" value="Pitfalls" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#EF4444;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="180" y="297" width="60" height="22" as="geometry" />
        </mxCell>
        <mxCell id="32" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FECACA;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="55" y="330" width="370" height="110" as="geometry" />
        </mxCell>
        <mxCell id="33" value="比较陷阱" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="340" width="65" height="20" as="geometry" />
        </mxCell>
        <mxCell id="34" value="包装类比较永远用 equals(),严禁依赖 ==" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#991B1B;spacingLeft=6;" vertex="1" parent="1">
          <mxGeometry x="135" y="338" width="280" height="24" as="geometry" />
        </mxCell>
        <mxCell id="35" value="性能陷阱" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="370" width="65" height="20" as="geometry" />
        </mxCell>
        <mxCell id="36" value="避免在循环中频繁拆装箱" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#991B1B;spacingLeft=6;" vertex="1" parent="1">
          <mxGeometry x="135" y="368" width="160" height="24" as="geometry" />
        </mxCell>
        <mxCell id="37" value="sum += i" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontStyle=1;fontColor=#991B1B;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="300" y="370" width="70" height="20" as="geometry" />
        </mxCell>
        <mxCell id="38" value="精度陷阱" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="400" width="65" height="20" as="geometry" />
        </mxCell>
        <mxCell id="39" value="浮点数运算会丢失精度" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#991B1B;spacingLeft=6;" vertex="1" parent="1">
          <mxGeometry x="135" y="398" width="150" height="24" as="geometry" />
        </mxCell>
        <mxCell id="40" value="0.1 + 0.2 ≠ 0.3" style="text;html=1;strokeColor=none;fillColor=#FEE2E2;align=center;verticalAlign=middle;fontSize=11;fontFamily=Courier New;fontStyle=1;fontColor=#991B1B;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="290" y="400" width="100" height="20" as="geometry" />
        </mxCell>
        <mxCell id="41" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FAF5FF;strokeColor=#8B5CF6;strokeWidth=3;" vertex="1" parent="1">
          <mxGeometry x="460" y="280" width="400" height="175" as="geometry" />
        </mxCell>
        <mxCell id="42" value="🧮 特殊处理" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=15;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="475" y="293" width="120" height="30" as="geometry" />
        </mxCell>
        <mxCell id="43" value="Advanced" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=11;fontColor=#8B5CF6;fontStyle=2;" vertex="1" parent="1">
          <mxGeometry x="600" y="297" width="70" height="22" as="geometry" />
        </mxCell>
        <mxCell id="44" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#E9D5FF;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="475" y="330" width="370" height="110" as="geometry" />
        </mxCell>
        <mxCell id="45" value="金额计算" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="340" width="65" height="20" as="geometry" />
        </mxCell>
        <mxCell id="46" value="必须使用 BigDecimal(构造参数传 String)" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#6D28D9;spacingLeft=6;" vertex="1" parent="1">
          <mxGeometry x="555" y="338" width="280" height="24" as="geometry" />
        </mxCell>
        <mxCell id="47" value="超大整数" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="370" width="65" height="20" as="geometry" />
        </mxCell>
        <mxCell id="48" value="超出 long 范围使用 BigInteger" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#6D28D9;spacingLeft=6;" vertex="1" parent="1">
          <mxGeometry x="555" y="368" width="220" height="24" as="geometry" />
        </mxCell>
        <mxCell id="49" value="溢出处理" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="485" y="400" width="65" height="20" as="geometry" />
        </mxCell>
        <mxCell id="50" value="注意最大值限制" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;fontSize=12;fontColor=#6D28D9;spacingLeft=6;" vertex="1" parent="1">
          <mxGeometry x="555" y="398" width="110" height="24" as="geometry" />
        </mxCell>
        <mxCell id="51" value="int 21亿" style="text;html=1;strokeColor=none;fillColor=#F3E8FF;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#6D28D9;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="670" y="400" width="60" height="20" as="geometry" />
        </mxCell>
        <mxCell id="52" value="long 19位" style="text;html=1;strokeColor=none;fillColor=#F3E8FF;align=center;verticalAlign=middle;fontSize=10;fontStyle=1;fontColor=#6D28D9;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="740" y="400" width="70" height="20" as="geometry" />
        </mxCell>
        <mxCell id="53" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFBEB;strokeColor=#F59E0B;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="40" y="475" width="820" height="130" as="geometry" />
        </mxCell>
        <mxCell id="54" value="📋 快速选择指南" style="text;html=1;strokeColor=none;fillColor=#F59E0B;align=center;verticalAlign=middle;fontSize=14;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="60" y="490" width="140" height="28" as="geometry" />
        </mxCell>
        <mxCell id="55" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FDE68A;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="55" y="530" width="180" height="60" as="geometry" />
        </mxCell>
        <mxCell id="56" value="局部变量 / 循环" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#92400E;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="538" width="100" height="20" as="geometry" />
        </mxCell>
        <mxCell id="57" value="→ 基本类型" style="text;html=1;strokeColor=none;fillColor=#3B82F6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="65" y="563" width="80" height="20" as="geometry" />
        </mxCell>
        <mxCell id="58" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FDE68A;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="250" y="530" width="180" height="60" as="geometry" />
        </mxCell>
        <mxCell id="59" value="集合 / 泛型 / 可空" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#92400E;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="260" y="538" width="110" height="20" as="geometry" />
        </mxCell>
        <mxCell id="60" value="→ 包装类型" style="text;html=1;strokeColor=none;fillColor=#22C55E;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="260" y="563" width="80" height="20" as="geometry" />
        </mxCell>
        <mxCell id="61" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FDE68A;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="445" y="530" width="180" height="60" as="geometry" />
        </mxCell>
        <mxCell id="62" value="金额 / 精确计算" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#92400E;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="455" y="538" width="100" height="20" as="geometry" />
        </mxCell>
        <mxCell id="63" value="→ BigDecimal" style="text;html=1;strokeColor=none;fillColor=#8B5CF6;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="455" y="563" width="95" height="20" as="geometry" />
        </mxCell>
        <mxCell id="64" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#FFFFFF;strokeColor=#FDE68A;strokeWidth=1;" vertex="1" parent="1">
          <mxGeometry x="640" y="530" width="205" height="60" as="geometry" />
        </mxCell>
        <mxCell id="65" value="超大数 / 密码学" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#92400E;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="650" y="538" width="100" height="20" as="geometry" />
        </mxCell>
        <mxCell id="66" value="→ BigInteger" style="text;html=1;strokeColor=none;fillColor=#EF4444;align=center;verticalAlign=middle;fontSize=11;fontStyle=1;fontColor=#FFFFFF;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="650" y="563" width="90" height="20" as="geometry" />
        </mxCell>
        <mxCell id="67" value="" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#F0FDF4;strokeColor=#22C55E;strokeWidth=2;" vertex="1" parent="1">
          <mxGeometry x="40" y="625" width="820" height="70" as="geometry" />
        </mxCell>
        <mxCell id="68" 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="60" y="640" width="100" height="26" as="geometry" />
        </mxCell>
        <mxCell id="69" value="能用基本类型就用基本类型(性能好)" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#166534;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="175" y="642" width="250" height="24" as="geometry" />
        </mxCell>
        <mxCell id="70" value="包装类比较用 equals()" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#166534;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="440" y="642" width="160" height="24" as="geometry" />
        </mxCell>
        <mxCell id="71" value="金额用 BigDecimal" style="text;html=1;strokeColor=none;fillColor=#DCFCE7;align=center;verticalAlign=middle;fontSize=12;fontStyle=1;fontColor=#166534;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="615" y="642" width="140" height="24" as="geometry" />
        </mxCell>
        <mxCell id="72" value="POJO 属性用包装类型(阿里规范)" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=11;fontColor=#92400E;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="280" y="670" width="220" height="20" as="geometry" />
        </mxCell>
        <mxCell id="73" value="避免循环中装箱" style="text;html=1;strokeColor=none;fillColor=#FEF3C7;align=center;verticalAlign=middle;fontSize=11;fontColor=#92400E;rounded=1;" vertex="1" parent="1">
          <mxGeometry x="515" y="670" width="110" height="20" as="geometry" />
        </mxCell>
      </root>
    </mxGraphModel>
  </diagram>
</mxfile>