BoxedNull
DRAFT

Misconception:

Variables of wrapper types (like Boolean, Integer, Double, …) wrap around a value of the corresponding primitive type (like boolean, int, double, …), or they wrap around null. E.g., with a boolean we can only represent true or false, but with a Boolean we have three options: new Boolean(true), new Boolean(false), and new Boolean(null).

Incorrect

Passing null to a wrapper class constructor creates an object representing the absence of a value

Correct

Wrapper class constructors expect a value of a primitive type, and null is not such a value

Correction
Here is what's right.

It is correct that with a wrapper type we have the ability to denote the absence of a value, and with a primitive type, we cannot do that. However, to denote the absence of a value, we have to use null, not new Boolean(null):

Boolean b;
b = null; // we don't have a value
b = new Boolean(true); // we have a value, and it is true
b = new Boolean(false); // we have a value, and it is false

Origin
Where could this misconception come from?

Students may have heard that they can use wrapper classes in cases where they want to represent an optional value of a primitive type. For example, students might encounter the following situation:

// represent whether a person is married or not, if known
boolean isMarried;
isMarried = true; // ok
isMarried = false; // ok
isMarried = null; // illegal, null is not of type boolean!

So students might want to use Boolean:

Boolean isMarried;
isMarried = true;  // ok, uses auto boxing
isMarried = false; // ok, uses auto boxing
isMarried = null;  // ok, Boolean is a reference type

But they don’t fully understand and they instead write:

Boolean isMarried;
isMarried = new Boolean(true);  // ok
isMarried = new Boolean(false); // ok
isMarried = new Boolean(null);  // illegal, null is not of type boolean!

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.