Re: Changing the value of Boolean?

From: xarax (xarax_at_email.com)
Date: 02/20/04


Date: Fri, 20 Feb 2004 19:16:14 GMT


"Christophe Vanfleteren" <c.v4nfl3t3r3n@pandora.be> wrote in message
news:E6qZb.7848$Ai1.445545@phobos.telenet-ops.be...
> Blueyonder wrote:
>
> > Sure I've read somewhere (but can't find it now) that you cannot change
> > the value of a Boolean tyep variable. You have to set it each time like
> > this -
> >
> > ////////////////////////////////////////////////////////////
> > Boolean b = new Boolean(true);
> >
> > b = new Boolean(false);
> > ////////////////////////////////////////////////////////////
> >
> > Is this correct?
> >
> > thanks
> >
> > harry
>
> Yes, once a Boolean is created, it's value can not be changed (as is the
> case with all "wrapper" types, such as Integer, Long, ...). We call such
> classes immutable.
>
> But in the case of boolean, instead of creating a new Boolean using
> something like "new Boolean(false)", use the Boolean b = Boolean.FALSE
> instead.
>
> It prevents having to create an extra Boolean object, which would behave
> exactly the same as new Boolean(false) anyway.
>
> --
> Kind regards,
> Christophe Vanfleteren

Exactly right. A simple idiom is:

{
   boolean bool = methodThatReturnsPrimitiveBoolean();
   Boolean theBoolean;

   /* Translate boolean primitive to Boolean instance. */
   theBoolean = (bool ? Boolean.TRUE : Boolean.FALSE);
}



Relevant Pages

  • Re: Changing the value of Boolean?
    ... Blueyonder wrote: ... Yes, once a Boolean is created, it's value can not be changed (as is the ... It prevents having to create an extra Boolean object, which would behave ... Kind regards, ...
    (comp.lang.java.programmer)
  • Re: Formula Combination
    ... is TRUE meant to be a text or the Boolean result of a formula? ... Kind regards, ... Randy Street ... Rancho Cucamonga, CA ...
    (microsoft.public.excel.worksheet.functions)
  • Re: ruby and true
    ... I have some code the checks whether some text is on the HTML page ... that it is usually a very bad idea to try to compare a boolean value with a boolean constant in a boolean context. ... This applies to all sorts of ... Kind regards ...
    (comp.lang.ruby)
  • Re: if the else short form
    ... Boolean values behave like the values 0 and 1, respectively, in ... almost all contexts, the exception being that when converted to a ...
    (comp.lang.python)