• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Boxing conversions question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice that b3 is a primitive. When you use the == between a Wrapper and a primitive, the wrapper is unboxed.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See our SCJP FAQ: Why do separate autoboxing conversions sometimes return the same reference?.
 
reply
    Bookmark Topic Watch Topic
  • New Topic