If you check the Boolean class' API you will see that there are two instances of the class Boolean that are "readymade". One is a wrapper for true
and the other is a wrapper for false
. One of these two instances is returned by all of the valueOf methods. That's why "b1 == b2" and "b3 == b4" are true
in the example. In fact all of b1, b2, b3, b4 refer to the same wrapper object representing true
Again referring to the API, if you really want a new Boolean instance, independent of the two readymade ones, you should
use the Boolean constructor.
Boolean b5 = new Boolean(true). This results in a Boolean wrapper object such that b5 != b1 (or b2, b3, b4).
The API tells you all that and more besides.
[ September 23, 2005: Message edited by: Barry Gaunt ]