• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

When is new Boolean(true) required?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get that one should try to use Boolean.valueOf() instead of new Boolean() as much as possible since creating new instances of Boolean wastes memory.

However, the Boolean API for valueOf() states:

If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean(boolean), (...)



When would a new Boolean instance be required? Boolean objects are immutable, and there are only two useful values; Boolean.TRUE and Boolean.FALSE ..

Am I missing something here?
[ June 29, 2004: Message edited by: Mark Sintrel ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I personally can't think of a situation where one would be required.

Remember that the folks who wrote these APIs are only human. I think this is a case where they might reasonably have done things differently, but it's water under the bridge now -- can't remove the constructor without breaking existing code. Core classes like Boolean were written long ago, generally by just one person, without anything resembling an "Architecture Document" -- mostly just making things up as he (or she) went along.
 
Mark Sintrel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is true .. however, the person who wrote it seem to be aware of this, look at the Boolean constructor documentation:

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.



.. but I can't think of any use either.

Thanks for answering
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

One usage that I came across and made sense was in one place the new Boolean() was used to make comparisons by reference. See the example below (the method needed to return a boolean instance, not null for some reason - it was in a wider context that's difficult to explain here)

suppose you have:
static final Boolean EMPTY_BOOLEAN = new Boolean(false);

Boolean convertToBoolean(int i) {
if (i == 0) {
return Boolean.FALSE;
} else if (i == 1) {
return Boolean.TRUE;
}
return EMPTY_BOOLEAN;
}

and

....
Boolean b = convertToBoolean(i);
if (b == EMPTY_BOOLEAN) { //reference comparison
//issue a warning
}
...

So basically the only time you'd need to use the Boolean constructor is when you need some value to compare by reference to.

Cheers,
Andrei
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One might prefer to use "null" instead of EMPTY_BOOLEAN; this would have the advantage of being understood immediately, rather than only via reference to the documentation.

Anyway, as I said, they can't remove the constructor now, even though they've realized that it's useless, because removing it would break existing code. If you've got any older JDK Javadocs lying around, go have a look: you'll see that even as recently as JDK 1.3.1, the entire text of the Javadoc for this constructor was




The comment about the constructor being useless was added many years after the code was originally written.
[ June 30, 2004: Message edited by: Ernest Friedman-Hill ]
 
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic