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 - w开发者_如何学Gohy 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;
As the previous answers stated, Boolean.TRUE
returns the wrapper object of the boolean value true
, so for the contexts where we need to treat boolean's like objects (for example, having an ArrayList
of booleans), we could use Boolean.TRUE
or Boolean.FALSE
As for why:
boolean boolVar = Boolean.TRUE;
is valid is because of Autoboxing and Unboxing.
In short, the Java compiler, when it sees you treating a primitive like an object, such as
List<Boolean> listOfBoolean = new ArrayList<Boolean>();
boolean someBool = true;
listOfBoolean.add(someBool);
it will automatically wrap it, or autobox it
List<Boolean> listOfBoolean = new ArrayList<Boolean>();
boolean someBool = true;
listOfBoolean.add(Boolean.valueOf(someBool));
And if it sees you treating a wrapper object, like Boolean.TRUE
, as a primitive, like:
boolean boolVar = Boolean.TRUE;
it will convert it down into a primitive, or unbox it, like if we did:
boolean boolVar = Boolean.TRUE.booleanValue();
Once upon a time, you'd have to do this by hand, but now, for better or for worse, this is mostly taken care of for you.
And if you're wondering why have Boolean.TRUE
at all, it's because there is no need to have floating around lots of Boolean objects for true
. Since a boolean can only be one of two values, it's simpler just to have them as constants rather than, for every time someone needs boxed up true
:
Boolean trueBool = new Boolean(true);
Boolean.TRUE is a wrapper object and singleton . true is a literal constant. Below are 2 situations where I use wrappers over primitives
- I want to store them in Collections
- I'd want to have a notion of null. primitive boolean can only represent two states.
true
is of the primitive boolean
type while Boolean.TRUE
is a Boolean
object that wraps the true
value.
Primitive types, (e.i. boolean
), are strongly preferred over classes (e.i. Boolean
) for a number of reasons. See discussion here. https://softwareengineering.stackexchange.com/questions/203970/when-to-use-primitive-vs-class-in-java. Primitive type makes code more readable, prevents pointer errors, such as if(a==b)
vs if(a.equals(b))
, increases performance and follows the conversion.
There is one case when Boolean
or Integer
works better than boolean
and int
. That is if you have a situation when you want to allow null
as a value. This results in a number of null checks, but it prevents false
from slipping through when
Boolean.TRUE is a reference to an object of the class Boolean, while true is just a value of the primitive boolean type. Classes like Boolean are often called "wrapper classes", and are used when you need an object instead of a primitive type (for example, if you're storing it in a data structure).
You can also get an explanation on the link from Wikipedia below.
All of the primitive wrapper classes in Java are immutable. J2SE 5.0 introduced autoboxing of primitive types into their wrapper object, and automatic unboxing of the wrapper objects into their primitive value—the implicit conversion between the wrapper objects and primitive values.
More on http://en.wikipedia.org/wiki/Primitive_wrapper_class
精彩评论