Alex Rivera | Logout

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

Asked 2011-02-04T03:35:24.613
43

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.

Edit
Report

1 Answer

40

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;
answered 2011-02-04T03:42:38.150

Your Answer