• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ){};
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic