「こんにちは、アミーゴ!」

「こんにちは、キム。」

「ブール型について説明します。これはブール型のラッパーであり、非常に簡単です。ブール クラスの簡略化されたコードを次に示します。」

コード
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 に対応する 2 つの定数 (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)

今のところはこれだけだと思います。

「ええ、あなたのレッスンはビラーボよりも短いです。」

「それでは、if 条件内で Boolean を使用できますか?」

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

「はい、less が null の場合は NullPointerException がスローされることを忘れないでください。」

「ええ、それはもうわかっています。ただ、それを常に頭の中に入れておくわけではないのです。」