Changing the value of Boolean?

Discussion in 'Java' started by Blueyonder, Feb 20, 2004.

  1. Blueyonder

    Blueyonder Guest

    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
     
    Blueyonder, Feb 20, 2004
    #1
    1. Advertisements

  2. 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.
     
    Christophe Vanfleteren, Feb 20, 2004
    #2
    1. Advertisements

  3. Blueyonder

    Alex Hunsley Guest

    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
     
    Alex Hunsley, Feb 20, 2004
    #3
  4. Blueyonder

    xarax Guest

    Exactly right. A simple idiom is:

    {
    boolean bool = methodThatReturnsPrimitiveBoolean();
    Boolean theBoolean;

    /* Translate boolean primitive to Boolean instance. */
    theBoolean = (bool ? Boolean.TRUE : Boolean.FALSE);
    }
     
    xarax, Feb 20, 2004
    #4
  5. 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, Feb 20, 2004
    #5
  6. 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 ==
     
    Thomas Schodt, Feb 20, 2004
    #6
  7. Blueyonder

    Dale King Guest

    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.
     
    Dale King, Feb 21, 2004
    #7
  8. Good point. Thanks.

    = Steve =
     
    Steve W. Jackson, Feb 23, 2004
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.