tags:

views:

193

answers:

5

For e.g,

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
   new Boolean(true));

JAXB Tutorial

+3  A: 

I don't think there's a reason in particular. That's pretty poor practice, IMO. Boolean.TRUE and Boolean.FALSE do everything you need.

Malaxeur
Maybe the fear of the singleton...
Geo
And `Boolean.valueOf(boolean)` works when you don't know what you need.
Joachim Sauer
Yeah. The huge problem with the code above is that it first creates the true literal and then creates a boolean object wrapping the literal :(. It's like doing 'new String("banana")' for whatever reason.
Malaxeur
That's not the problem - the literal is a primitive, the wrapper an Object. Since the setProperty() method only takes objects, this is necessary, and if you depend on autoboxing, the same thing will happen implicitly. The problem (an insiginficant one here) is that by using the constructor, you create a new Boolean object each time, when two of them (Boolean.TRUE and Boolean.FALSE) are all you really need.
Michael Borgwardt
Although it's insignificant here, it's pretty painful when you have millions of 'True' Boolean wrappers, for no reason at all.
Malaxeur
+1  A: 

Nowadays, with autoboxing, you can write true and it will automatically be converted into new Boolean(true) if a Boolean object (instead of a boolean primitive) is expected.

However, in Java 1.4 and earlier, this is not possible; one had to create the Boolean object explicitly. The tutorial you link to is from 2003, and at the time Java 5.0 (a.k.a. Java 1.5) hadn't been released yet.

But even then, as Malaxeur noted, it would have been better to use Boolean.TRUE.

Thomas
it will actually be converted into Boolean.valueOf(booleanVar), which will never produce new instances but only ever use Boolean.TRUE and Boolean.FALSE.
Joachim Sauer
A: 

That's actually very strange as the Javadocs for boolean explicitly state it's generally not a good idea http://java.sun.com/javase/6/docs/api/java/lang/Boolean.html. Perhaps that tutorial is somewhat old?

Constructor Detail Boolean

public Boolean(boolean value)

Allocates a Boolean object representing the value argument.

Note: It is rarely appropriate to use this constructor. Unless a new

instance is required, the static factory valueOf(boolean) is generally a better choice. It is likely to yield significantly better space and time performance.

Parameters:
    value - the value of the Boolean
Falaina
A: 

Most of the time when you want a Boolean object, you need the capabilities of a reference type. A method might expect an object, so you pass a Boolean. But probably most important is a Boolean can be null as well as true and false. Say you had a bank with a Boolean transaction succeeded, true for if it did, false for if not. But what if the transaction didn't take place yet? You could set it to false, but that would be confusing as its not accurately describing the data.

This a case where a Boolean comes in handy, but it definitely doesn't mean that its always the right choice. :D

CrazyJugglerDrummer
A: 

Technically spoken, and as Malaxeur and Thomas argued, one should not use the Boolean(boolean value) constructor but use Boolean.TRUE and Boolean.FALSE instead. This is even documented in the JavaDoc since version 1.4.2, although the Boolean.TRUE and Boolean.FALSE objects are as old as jdk 1.0.1

I do not believe, that JAXB requires unique instances for property values. If that was the unlikely case, there would be an explicit warning to not use Boolean.TRUE.

The only reason I see: this is a tutorial and the code is improved towards readability. If one reads 'new Boolean(true)' it is plain clear, that a new Boolean instance is created. 'Boolean.TRUE' could be, technically spoke, any type and therefore doesn't make clear at first glance, that a Boolean object is needed in this method.

Andreas_D