simple java question

Discussion in 'OT Technology' started by HardTech, Dec 6, 2006.

  1. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    Code:
    boolean b;
    System.out.println(b = false);
    this prints out false. Why?

    Shouldn't it print out true as long as "b = false" executes?
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    the equal sign returns a reference to the object b. the value of b, since "b = false" gets executed first, is false. to get a return type of a boolean value you have to use ==. the assignment operator does not have an additional boolean return type.
     
  3. sprite

    sprite Active Member

    Joined:
    May 20, 2003
    Messages:
    2,967
    Likes Received:
    0
    Location:
    MI, USA
    Default of b is false.

    "b = false" sets the value of b to false.

    b == false evaluates the equality of b to false, which should = true.
     
  4. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    Do you mean that if I just instantiate boolean b and never assign a value to it, it's false?

    Isn't it null?
     
  5. sprite

    sprite Active Member

    Joined:
    May 20, 2003
    Messages:
    2,967
    Likes Received:
    0
    Location:
    MI, USA
    boolean is primitive and cannot be null. Only objects can be null.
     
  6. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    so the following code:
    Code:
    boolean b;
    System.out.println(b);
    
    should print out false?
     
  7. sprite

    sprite Active Member

    Joined:
    May 20, 2003
    Messages:
    2,967
    Likes Received:
    0
    Location:
    MI, USA
    Bah, getting my languages messed up.

    That statement will complain that b was not initialized.

    It is worth noting though that a boolean can't be null, can't be set to null, and can't be evaluated against null.
     
  8. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    :coold:

    thanks :bigthumb:
     
  9. Doneranator

    Doneranator New Member

    Joined:
    Nov 16, 2006
    Messages:
    60
    Likes Received:
    0
    in java boolean is default false, it has to be set to true.
     
  10. AznRyda

    AznRyda キモかわいい!

    Joined:
    May 6, 2000
    Messages:
    28,632
    Likes Received:
    0
    Location:
    残業中
    Man I thought you were a programmer. :o
     

Share This Page