user183037 user183037 - 5 months ago 19
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.


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;