aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boxing - Boolean question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Boxing - Boolean question" Watch "Boxing - Boolean question" New topic
Author

Boxing - Boolean question

David Attard
Greenhorn

Joined: May 13, 2003
Posts: 26


Answer:
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?


There's no place like 127.0.0.1
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: May 13, 2003
Posts: 26
Great, thanks.
Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
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

Joined: Oct 31, 2006
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Oct 31, 2006
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

Joined: Feb 07, 2005
Posts: 2367
Did I explain the answer? I wasn't sure if you were asking again.
saqib sarwar
Ranch Hand

Joined: Mar 30, 2007
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


SCJP5 95%, SCWCD 85%<br />Knowledge is the Life of Mind
Sourin K. Sen
Ranch Hand

Joined: May 02, 2006
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.
please explain
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 ]

Regards,<br />Sourin.
Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
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 .
 
Consider Paul's rocket mass heater.
 
subject: Boxing - Boolean question