“嗨，阿米戈！”

“嗨，金。”

“我將向您介紹布爾類型。它是布爾類型的包裝器，非常簡單。這是布爾類的一些簡化代碼：”

``````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。”

“是的，我已經明白了。我只是不會一直把它記在腦子裡。”