44

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.

javanna
  • 59,145
  • 14
  • 144
  • 125
user183037
  • 2,549
  • 4
  • 31
  • 42
  • http://stackoverflow.com/questions/4531767/what-is-the-difference-between-the-boolean-object-and-the-boolean-data-type-in-ja – dreadwail Feb 04 '11 at 03:39
  • The link doesn't answer why `boolean boolVar = Boolean.TRUE;` is a valid, meaningful statement. I say meaningful because it does what it looks like it does :) – user183037 Feb 04 '11 at 03:44
  • 1
    That is because the link addressed the question you asked prior to editing. – dreadwail Feb 09 '11 at 22:09
  • I also made the comment after my edit - I was just clarifying that it wasn't what I was looking for. I wasn't saying you were wrong - I was just saying I wasn't looking for what you linked to. – user183037 Feb 11 '11 at 02:25

7 Answers7

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;
ColinD
  • 108,630
  • 30
  • 201
  • 202
  • Excellent - I forgot all about autounboxing! But why have Boolean.TRUE in the first place? – user183037 Feb 04 '11 at 03:48
  • 1
    @user183037: So you can use the object directly when you need to rather than having to go through autoboxing. As far as the constant itself, you want all instances of `Boolean` in a system to be one of two objects, `Boolean.TRUE` or `Boolean.FALSE`. As long as you don't use the constructor for `Boolean`, this is the case. – ColinD Feb 04 '11 at 03:51
  • 2
    More to the point, you want only a single Boolean object in the VM with a `true` value. Similarly for `false`. In other words, two Boolean objects that are `equal()` are also `==`--that is, the same (identical) object. – Ted Hopp Feb 04 '11 at 04:42
  • @Ted: Yes, that's pretty much what I was saying. – ColinD Feb 04 '11 at 04:50
  • 1
    So much for "Java does not support operator overloading." This is exactly what operator overloading would have achieved... – aggregate1166877 Apr 10 '13 at 08:26
36

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); 
Zach L
  • 16,072
  • 4
  • 38
  • 39
  • 1
    I don't seem to be able to accept 2 answers, so I accepted Colin's as he posted earlier, but your detailed explanation made a lot of sense - thank you! – user183037 Feb 04 '11 at 03:58
  • @user183037 That's fine, I'm just glad to help :-) – Zach L Feb 04 '11 at 03:59
8

Boolean.TRUE is a wrapper object and singleton . true is a literal constant. Below are 2 situations where I use wrappers over primitives

  1. I want to store them in Collections
  2. I'd want to have a notion of null. primitive boolean can only represent two states.
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
3

true is of the primitive boolean type while Boolean.TRUE is a Boolean object that wraps the true value.

casablanca
  • 69,683
  • 7
  • 133
  • 150
1

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

Community
  • 1
  • 1
sixtytrees
  • 1,156
  • 1
  • 10
  • 25
0

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

Willy Makend
  • 137
  • 1
  • 8
0

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).

Chris Smith
  • 2,615
  • 19
  • 18