user183037 user183037 - 1 year ago 122
Java Question

What is the difference between Boolean.TRUE and true in Java?

PS: I understand the difference between "true" and true.

I also understand that Boolean.TRUE is a wrapper for the primitive true, my question then is - why does the primitive boolean accept Boolean.TRUE as a value?
For instance,

boolean boolVar = Boolean.TRUE;

seems to be a valid statement.

Answer Source

The reason

boolean boolVar = Boolean.TRUE;

works is because of autounboxing, a Java 5 feature that allows a wrapper object to be converted to its primitive equivalent automatically when needed. The opposite, autoboxing, is also possible:

Boolean boolVar = true;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download