Changing the value of Boolean?  
Author Message
Blueyonder





PostPosted: 2004-2-20 23:47:00 Top

java-programmer, Changing the value of Boolean? 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


 
Christophe Vanfleteren





PostPosted: 2004-2-20 23:56:00 Top

java-programmer >> Changing the value of Boolean? 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
 
Alex Hunsley





PostPosted: 2004-2-20 23:57:00 Top

java-programmer >> Changing the value of Boolean? 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

Yup. If you look at the API doc for boolean, you can see that it is
immutable (i.e. can't be changed once you've made it).

Please post simpler questions like this to comp.lang.java.help btw, as
it's more suited...

alex


 
 
xarax





PostPosted: 2004-2-21 3:16:00 Top

java-programmer >> Changing the value of Boolean? "Christophe Vanfleteren" <email***@***.com> wrote in message
news:E6qZb.7848$email***@***.com...
> 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);
}



 
 
Steve W. Jackson





PostPosted: 2004-2-21 4:08:00 Top

java-programmer >> Changing the value of Boolean? In article <22tZb.2104$email***@***.com>,
"xarax" <email***@***.com> wrote:

>:"Christophe Vanfleteren" <email***@***.com> wrote in message
>:news:E6qZb.7848$email***@***.com...
>:> 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);
>:}

I agree with the recommendation to use the static member variables of
Boolean as described. But Sun's source code for the static variables
TRUE and FALSE in the Boolean class are both defined using the "new
Boolean" operation. So it's still necessary to create a new object on
the JVM's heap.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
 
Thomas Schodt





PostPosted: 2004-2-21 4:42:00 Top

java-programmer >> Changing the value of Boolean? Steve W. Jackson wrote:

> I agree with the recommendation to use the static member variables of
> Boolean as described. But Sun's source code for the static variables
> TRUE and FALSE in the Boolean class are both defined using the "new
> Boolean" operation. So it's still necessary to create a new object on
> the JVM's heap.

But just once.

As it is static initialization, Boolean.TRUE and Boolean.FALSE will
reference two objects that get created when the Boolean class is loaded.

From then on the same two objects are just re-used.
You will even be able to compare them with ==
 
 
Dale King





PostPosted: 2004-2-21 11:06:00 Top

java-programmer >> Changing the value of Boolean? "xarax" <email***@***.com> wrote in message
news:22tZb.2104$email***@***.com...
> > 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.

> Exactly right. A simple idiom is:
>
> /* Translate boolean primitive to Boolean instance. */
> theBoolean = (bool ? Boolean.TRUE : Boolean.FALSE);

And starting with JDK1.4 you no longer have to do that. There is now a
Boolean.valueOf( boolean ) method that does exactly the same thing.


 
 
Steve W. Jackson





PostPosted: 2004-2-24 4:47:00 Top

java-programmer >> Changing the value of Boolean? In article <c15re8$ah2$1$email***@***.com>,
Thomas Schodt <"news04jan"@\"xenoc.demon.co.uk\"> wrote:

>:Steve W. Jackson wrote:
>:
>:> I agree with the recommendation to use the static member variables of
>:> Boolean as described. But Sun's source code for the static variables
>:> TRUE and FALSE in the Boolean class are both defined using the "new
>:> Boolean" operation. So it's still necessary to create a new object on
>:> the JVM's heap.
>:
>:But just once.
>:
>:As it is static initialization, Boolean.TRUE and Boolean.FALSE will
>:reference two objects that get created when the Boolean class is loaded.
>:
>: From then on the same two objects are just re-used.
>:You will even be able to compare them with ==

Good point. Thanks.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama