Boolean vs boolean

Boolean is an primitive type and thus can not be null.

The Boolean object is a wrapper class for the primitive type boolean. Because it’s and object it can also be null, which is not possible in boolean algebra. This does make it seem less logical.

When using the Boolean type the JVM converts this to boolean with autoboxing.

We use Boolean like this.

Boolean a = Boolean.TRUE;
Boolean a1 = Boolean.FALSE;

Even tho it is possible to instantiate a Boolean like this.

Boolean a2 = new Boolean(true);

We don’t use it like this, because it instantiates a new Boolean object every time while there are only 2 types possible. This is also the only way we can create a null Boolean object. If we instantiate with constants we prevent getting a null Boolean object.

We use boolean like this.

boolean b = true;
boolean b1 = false;

To convert to and from booleans we use the Boolean class, because primitive types don’t have methods.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s