user183037 user183037 - 3 months ago 15
Java Question

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

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

Edit:
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

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;
Comments