This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why b is not boxed to wrapper Boolean to hold the value null. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why b is not boxed to wrapper Boolean to hold the value null." Watch "why b is not boxed to wrapper Boolean to hold the value null." New topic
Author

why b is not boxed to wrapper Boolean to hold the value null.

pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
hi
Why the given below code is not legal?
answer is: null value is not compatible to boolean primitive type.
But my question is why b is not boxed to wrapper Boolean to hold the value null.

code is:


boolean b = null ;
if( b == null ){};


SCJP 5.0(75%), SCWCD 5.0(88%)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19342
    
  41

The b reference is a primative variable. How do you expect it to hold an object? And even if you can box null to a Boolean, how do you expect a primative variable to hold a Boolean object?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
Yes you are right Mr.Henry Wong and i know this But my question is why primitive b can not boxed to Boolean b.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19342
    
  41

Originally posted by pradeep singh:
Yes you are right Mr.Henry Wong and i know this But my question is why primitive b can not boxed to Boolean b.


Boxing is for objects -- not variables. Primative values get boxed to objects so that they can be assigned to referemces. Objects get unboxed so that they can be assigned to primative variables.

Although, it may seem like it does, it doesn't change a primative variable into a reference.

Henry
 
rocket mass heater kickstarter
 
subject: why b is not boxed to wrapper Boolean to hold the value null.