## 1.包装器类型列表

``byte``
``Byte``
``short``
``Short``
``int``
``Integer``
``long``
``Long``
``float``
``Float``
``double``
``Double``
``char``
``Character``
``boolean``
``Boolean``

``````public class Integer
{
private int value;

Integer(int x)
{
this.value = x;
}

public int intValue()
{
return this.value;
}

public static Integer valueOf(int x)
{
return new Integer(x);
}
}``````

## 2. 将 an 转换`int`为 an`Integer`

``Integer name = new Integer(value);``

``Integer age = new Integer(18);``
``Integer hundred = new Integer(100);``
``Integer zero = new Integer(0);``

``int name = variable.intValue();``

``````Integer age = new Integer(18);
int x = age.intValue();``````
``````
x == 18``````
``````Integer hundred = new Integer(100);
int y = hundred.intValue();``````
``````
y == 100``````
``````Integer zero = new Integer(0);
int z = zero.intValue();``````
``````

z == 0``````
``````int i = 110;
Integer zero = new Integer(i);
int z = zero.intValue();``````
``````
z == 110``````

## 3. 自动装箱和拆箱

``````Integer a = Integer.valueOf(5);
int b = a.intValue();
Integer c = new Integer(b + 5)``````

`Integer`

Java 的创造者是这么认为的，所以他们教编译器如何自动执行这些操作。`int`an到 an的自动转换`Integer`称为自动装箱（自动将值放入盒子中），反向操作（将 an 转换`Integer`为 an `int`）称为拆箱

``Integer a = 10;``
``Integer a = Integer.valueOf(10);``
``int b = a;``
``int b = a.intValue();``
``Integer c = a + b;``
``Integer c = Integer.valueOf(a.intValue() + b);``

``````Integer a = 5;
int b = a;
Integer c = a + b;
int d = a + b + c;``````
``````Integer a = Integer.valueOf(5);
int b = a.intValue();
Integer c = Integer.valueOf(a.intValue() + b);
int d = a.intValue() + b + c.intValue();``````
``````Integer a = 5;
int b = 5;

if (a == b)
{
...
}``````
``````Integer a = Integer.valueOf (5);
int b = 5;

if (a.intValue() == b)
{
...
}``````

## 4.比较包装器变量

``````Integer a = 1000;
Integer b = 1000;

System.out.println(a == b);``````
``````

false``````
``````Integer a = 1000;
Integer b = 1000;

System.out.println(a.equals(b));``````
``````

true``````

`a`变量`b`不存储`int`值。它们存储对对象的引用。这意味着记住如何正确比较它们很重要：

``````Integer a = 1000;
Integer b = 1000;
if (a == b)
{
...
}``````
``````Integer a = 1000;
Integer b = 1000;
if (a.equals(b))
{
...
}``````