A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Arduino in Action
this week in the
General Computing
forum!
A special promo:
Enter your blog post or vote on a blogger to be featured in an upcoming Journal
JavaRanch
»
Java Forums
»
Certification
»
Programmer Certification (SCJP/OCPJP)
Author
Boxing query
Pawanpreet Singh
Ranch Hand
Joined: Jun 12, 2005
Posts: 264
I like...
posted
Aug 22, 2006 11:15:00
0
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
posted
Aug 22, 2006 11:34:00
0
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
posted
Aug 22, 2006 11:37:00
0
15.21.1 Numerical Equality Operators == and !=
5.6.2 Binary Numeric Promotion
I agree. Here's the link:
http://aspose.com/file-tools
subject: Boxing query
Similar Threads
Boxing conversions question
Boolean doubt
Please explain boxing conversion
conditional operator precedence
Boolean ==
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter