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

Boolean == boolean

Joshua Fix
Ranch Hand

Joined: Sep 18, 2007
Posts: 57
I find this to be odd. Can anybody offer an explanation?



This prints "yes no".

Since the first statement results in true, I assume that the boolean gets boxed to Boolean, then the two Booleans are compared, which makes me think that the == method just compares the value true or false. But the second one returns false and destroys that theory.

I'm confused.


SCJP 5.0
Julio Eneriz
Greenhorn

Joined: Nov 28, 2007
Posts: 15
The second statement is false because you are comparing references, not the actual objects.

I think what happens in the first one is auto-unboxing, because if it was auto-boxing, you would have the same problem, so in order to compare, you unbox.

But I would have said both were false, so thanks for your example.
nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124

I believe that 'b' is actually pooling the TRUE object which the primitive b1 is also pointing to but without actually creating a 'new' object! Then b2 is using the 'new' to create it's own object which is a seperate reference, but a reference to a pointer to the TRUE.

I'm not a bytecode expert, but it seems that they're both using the constant TRUE:
ldc #3; //String TRUE
So their pointing to the same value that's been pooled, but through differing references? That's my take - wish I could be more empirical about HOW it's working. However, anytime you see 'new' it's going to fail the == comparison with another reference variable unless it was manually assigned to that variable (ie b3 = b2;// now both point to same ojbect through the same reference)
[ December 09, 2007: Message edited by: nico dotti ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

For more detail...

In the case of b == b1, a Boolean object is being compared to a boolean primitive. This is a "boolean equality," so according to JLS 15.21.2, the Boolean object is unboxed and primitive booleans are compared. Both have a value of "true," so the comparison is true.

In the case of b == b2, two Boolean objects are being compared. Note that these are separate objects, each created using "new" (as opposed to autoboxing). This is a "reference equality," so according to JLS 15.21.3, the comparison is false because these do not refer to the same object.


"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
Sugantha Jeevankumar
Ranch Hand

Joined: Jun 06, 2007
Posts: 92
Hey Marc,
b1 is declared to be a primitive boolean...so both comparisons are similar in nature.I think it should be some ambiguity involved with the object pool
[ December 09, 2007: Message edited by: sugantha Jeevankumar ]

SCJP 5.0
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by sugantha Jeevankumar:
Hey Marc,
b1 is declared to be a primitive boolean...so both comparisons are similar in nature.I think it should be some ambiguity involved with the object pool...

I'm sorry, I don't understand what you mean. One of these comparisons is a "boolean equality" and the other is a "reference equality" as described in the JLS. (Note that the second comparison does not involve the primitive b1, and neither of the wrapper instances are created with autoboxing.) So what are you saying?
[ December 10, 2007: Message edited by: marc weber ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boolean == boolean