aspose file tools*
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 Java 8 in Action this week in the Java 8 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: 18136
    
  39

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: 18136
    
  39

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why b is not boxed to wrapper Boolean to hold the value null.
 
Similar Threads
regarding Wrapper's unboxing
Chew on this!
simple doubt - Please help !!
About wrap class and ==,equals;
equals()