Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • 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 ){};
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic