“嗨，阿米戈！”

“嗨，金。”

“我将向您介绍布尔类型。它是布尔类型的包装器，非常简单。这是布尔类的一些简化代码：”

``````class Boolean
{
public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);

private final boolean value;

public Boolean(boolean value)
{
this.value = value;
}

public boolean booleanValue()
{
return value;
}

public static Boolean valueOf(boolean b)
{
return (b ? TRUE : FALSE);
}
}``````

“换句话说，该类只是布尔类型的包装器。”

“是的。它有两个常量（TRUE 和 FALSE），它们是原始值 true 和 false 的对应物。”

“它还能像冠军一样处理自动装箱：”

``````Boolean a = true;
Boolean b = true;
Boolean c = false;
boolean d = a;``````
``````Boolean a = Boolean.valueOf(true);
Boolean b = Boolean.valueOf(true);
Boolean c = Boolean.valueOf(false);
boolean d = a.booleanValue();``````

“这里是布尔和布尔类型之间的比较是如何工作的：”

``````boolean a = true;
Boolean b = true; //Will be equal to Boolean.TRUE
Boolean c = true; //Will be equal to Boolean.TRUE

a == b; //true (comparison based on primitive value)
a == c; //true (comparison based on primitive value)
b == c; //true (comparison based on references, but they point to the same object)``````

“如果你真的需要创建一个独立的布尔对象，你必须明确地创建它：

``````boolean a = true;
Boolean b = new Boolean(true); //A new Boolean object
Boolean c = true; //Will be equal to Boolean.TRUE

a == b; //true (comparison based on primitive value)
a == c; //true (comparison based on primitive value)
b == c; //false (comparison based on references; they point to different objects)``````

“我想现在就这些了。”

“是啊，你的课比 Bilaabo 的要短。”

“那么，我可以在 if 条件中使用布尔值吗？”

``````Boolean less = (2 < 3);
if (less)
{
….
}``````

“是的，只是不要忘记，如果 less 为 null，则会抛出 NullPointerException。”

“是的，我已经明白了。我只是不会一直把它记在脑子里。”