aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boxing conversions 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 conversions question" Watch "Boxing conversions question" New topic
Author

Boxing conversions question

Mahmoud Kamal
Greenhorn

Joined: Oct 22, 2007
Posts: 5
public class Boxing6 {
public static void main(String[] args) {
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
boolean b3 = true;
Boolean b4 = true;
System.out.println(b1==b2);
System.out.println(b1==b3);
System.out.println(b3 == b4);
System.out.println(b1 == b4);
}
}

based on the mock exam where I found this question, the answer to this question is:
false true true false

which I don't understand why?! Shouldn't the answer be:
false false true false ?

Thanks in advance
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Notice that b3 is a primitive. When you use the == between a Wrapper and a primitive, the wrapper is unboxed.
Akhil Maharaj
Ranch Hand

Joined: Sep 29, 2007
Posts: 63
Check this out


This code will not compile . Moral of the story I think is : Wrapper == primitive gets compiled only when both sides are compatible . After compilation is successful , During runtime - Wrapper gets unboxed .

Any comments on this theory?

[ October 22, 2007: Message edited by: Akhil Maharaj ]

[ October 22, 2007: Message edited by: Akhil Maharaj ]
[ October 22, 2007: Message edited by: Akhil Maharaj ]
Mahmoud Kamal
Greenhorn

Joined: Oct 22, 2007
Posts: 5
Originally posted by Keith Lynn:
Notice that b3 is a primitive. When you use the == between a Wrapper and a primitive, the wrapper is unboxed.


Based on your reply then the following line should also return (true) since b1 will be un-boxed and b4 is already a primitive. But the question official answer says that this will return false....so why is this?
System.out.println(b1 == b4);

and if the above line returned false then the line
System.out.println(b1==b3);

should also return false! but that was not the case as it returned true!
I am confused here about this issue, so do you have any idea about the conflict in the above 2 lines?

Thanks in advance
Thirumalai Muthu
Ranch Hand

Joined: Oct 07, 2007
Posts: 75
b4 is not a primitive but a Boolean object because you have given it as
Boolean b4
which means Java automatically wraps b4 into an Boolean object.

Since b1 and b4 are Boolean objects and also they have the same value(true).In order to save Memory two instances of Boolean objects will always be == when their primitive values are same.
[ October 22, 2007: Message edited by: Thirumalai Muthu ]

SCJP 5
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See our SCJP FAQ: Why do separate autoboxing conversions sometimes return the same reference?.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Boxing conversions question