# Boxing - Boolean question

David Attard
Greenhorn
Posts: 26

false true true false

I've seen the question above in a mock exam and I'm not sure if I understand correctly. As I understand booleans are wrapped into immutable Boolean objects? Am I right? Can some please clarify?

Keith Lynn
Ranch Hand
Posts: 2409
The main thing to remember is what == does.

In this case, if one of the operands is a boolean primitive, and the other a Boolean, then the Boolean will be unboxed.

However, if both of the operands are Boolean, then == tests whether they are references to the same object.

David Attard
Greenhorn
Posts: 26
Great, thanks.

Vivian Josh
Ranch Hand
Posts: 112
Originally posted by Keith Lynn:
The main thing to remember is what == does.

In this case, if one of the operands is a boolean primitive, and the other a Boolean, then the Boolean will be unboxed.

However, if both of the operands are Boolean, then == tests whether they are references to the same object.

Vivian Josh
Ranch Hand
Posts: 112
Originally posted by Keith Lynn:
The main thing to remember is what == does.

In this case, if one of the operands is a boolean primitive, and the other a Boolean, then the Boolean will be unboxed.

However, if both of the operands are Boolean, then == tests whether they are references to the same object.

Hi Keith,

So you are saying that if x and y are two primitives then compare value and if they are both objects then compare identities.

If one of the item is object and other is primitive then unboxing of the object is done and their values are compared.

If this is true, then we should have following ans to the above question:

Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
boolean b3 = true;
Boolean b4 = true;
System.out.println(b1==b2);
// this should be FALSE as bothe are objects and hence separate references.

System.out.println(b1==b3);
// this should be TRUE as values are compared.

System.out.println(b3 == b4);
// this should be TRUE as values of the primitives are compared.

System.out.println(b1 == b4);
// this also should be TRUE as b1 is auto unboxed and values are compared.

But I see False True True False as the answer in original post. Can you explain me how the 4th SOP is False?

- v joshi

Keith Lynn
Ranch Hand
Posts: 2409
It is true that true is autoboxed into a Boolean object that is referred to as b4.

However, this is a different object than the Boolean created by

Vivian Josh
Ranch Hand
Posts: 112
My point is comparing b1==b3 and b1==b4 should result in the same answer . Thats not happening here. If in the first case b1 is unboxed and primitives are compared then why not in the second case as well? Why in the second case b4 is autoboxed and references are compared? Or should we assume this from the options given?

Many Thanks.

Keith Lynn
Ranch Hand
Posts: 2409
Did I explain the answer? I wasn't sure if you were asking again.

saqib sarwar
Ranch Hand
Posts: 77
Dear Joshi,

Boolean b4 = true;

is not a primitive its an wrapper object.

So keep your eyes Widen while dealing with boxing.

Regards

Sourin K. Sen
Ranch Hand
Posts: 86
Originally posted by sharan vasandani:
hi,

i have learnt that == returns true for different Boolean objects with same value from SCJP 5 Book by bates & kathy.
regards,
Sharan...

That would be the case when the objects are created using the new operator like this :

The sop here will return false because b1 & b2 are references referring to different objects.
However, when you create boolean objects like this :

The sop here will return true because in this case only one object has been created in the pool with the value true & both the references point to the same object.

In order to save memory, two instances of the following wrapper objects will always be == when their primitive values are the same:
Boolean
Byte
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127

Hope this helps.

Regards,
Sourin.
[ April 10, 2007: Message edited by: Sourin K. Sen ]

Vivian Josh
Ranch Hand
Posts: 112
Thanks Saqib,

I got what I was missing. Now have to be more careful while reading the question. I was reading Boolean as boolean hence getting wrong answer .

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.