aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boxing query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Boxing query" Watch "Boxing query" New topic
Author

Boxing query

Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Why the output is
/*

false
true
true
false

*/

for


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);


Could anybody explain....
Leonardo Luiz
Ranch Hand

Joined: Mar 24, 2006
Posts: 51
Hi,

I think it is because:

(b1==b2) and (b1==b4) compares two reference variable pointing to two diferent Boolean instances. (true is boxed into b4);

(b1==b3) compares two primitives (b1 is unboxed).

(b3==b4) compares two primitives.

wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
15.21.1 Numerical Equality Operators == and !=

5.6.2 Binary Numeric Promotion
 
Don't get me started about those stupid light bulbs.
 
subject: Boxing query