This discussion is archived
9 Replies Latest reply: May 9, 2012 8:11 AM by gimbal2 RSS

Difference between false and Boolean.FALSE ?

bootstrap Newbie
Currently Being Moderated
Hello,
can you please tell the difference between false and Boolean.FALSE ?
  • 1. Re: Difference between false and Boolean.FALSE ?
    796440 Guru
    Currently Being Moderated
    As you can see from the javadocs (http://docs.oracle.com/javase/6/docs/api/java/lang/Boolean.html#FALSE), Boolean.FALSE refers to a Boolean object. And as you can learn in any beginning Java tutorial or text, false is a primitive.
  • 2. Re: Difference between false and Boolean.FALSE ?
    912896 Newbie
    Currently Being Moderated
    Actually, Both.FALSE returns a primitive value of "false".

    You can use both of them because "in the practice", they don't have difference since both return a primitive value.
  • 3. Re: Difference between false and Boolean.FALSE ?
    796440 Guru
    Currently Being Moderated
    user13382622 wrote:
    Actually, Both.FALSE returns a primitive value of "false".
    No, it doesn't "return" anything. Boolean.FALSE is a reference to an object. It is not a primitive. It says this in the javadocs, which I linked to in my first reply (and which both you and the OP should have consulted before posting here).
    You can use both of them
    Yes.
    because "in the practice", they don't have difference since both return a primitive value.
    Wrong.
  • 4. Re: Difference between false and Boolean.FALSE ?
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    jverd wrote:
    user13382622 wrote:
    because "in the practice", they don't have difference since both return a primitive value.
    Wrong.
    What user13382622 meight be thinking of is autoboxing which lets false and Boolean.FALSE behave the same when assigning them to variables/references, or passing them as parameter.

    bye
    TPD
  • 5. Re: Difference between false and Boolean.FALSE ?
    796440 Guru
    Currently Being Moderated
    T.PD wrote:
    jverd wrote:
    user13382622 wrote:
    because "in the practice", they don't have difference since both return a primitive value.
    Wrong.
    What user13382622 meight be thinking of is autoboxing
    Yes, I'm sure he was, but he didn't express it clearly, and what he did state was flat-out wrong. I invite him to post again to clear up what he was trying to say.
  • 6. Re: Difference between false and Boolean.FALSE ?
    912896 Newbie
    Currently Being Moderated
    I'm sorry for didn't explain it clearly.

    T.PD is right, I was thinking on autoboxing way. But as jverd said, it doesn't "return" a primitive, and in fact I said in that way, so I wasn't clearly.
    For the main "issue" of this thread, in practice you can use both of them like it didn't have any difference.

    I think it is more clear now.
  • 7. Re: Difference between false and Boolean.FALSE ?
    796440 Guru
    Currently Being Moderated
    user13382622 wrote:
    For the main "issue" of this thread, in practice you can use both of them like it didn't have any difference.
    In many cases, yes, but not always. But since all the OP asked was "what's the difference?" the most appropriate answer is that one is an object and the other is a primitive.
  • 8. Re: Difference between false and Boolean.FALSE ?
    gimbal2 Guru
    Currently Being Moderated
    The trap behind autoboxing is that things work just fine when at least one side of the equation is a primitive. Things turn out unexpected when all of a sudden both sides of the equation happen to be an object:
    public class Test {
    
         public static void main(String[] args){
              
              boolean bp = false;
              Boolean bo = new Boolean(false);
              Boolean bo2 = new Boolean(false);
              
              System.out.println("Primitive VS Boolean.FALSE: " + (bp == Boolean.FALSE));
              System.out.println("Primitive VS new object: " + (bp == bo));
              System.out.println("New object VS Boolean.FALSE: " + (bo == Boolean.FALSE));
              System.out.println("new object vs new object 2: " + (bo == bo2));
         }
    }
    Result:
    Primitive VS Boolean.FALSE: true
    Primitive VS new object: true
    New object VS Boolean.FALSE: false
    new object vs new object 2: false
    Try explaining that in the context of this thread without ever referring to autoboxing specifically. Remember that people find these posts through Google, if you pass out information while hiding the gritty details, it gets confusing.
  • 9. Re: Difference between false and Boolean.FALSE ?
    796440 Guru
    Currently Being Moderated
    Also, null can bite you if you're careless.

    And there are cases where method signatures (and possibly return types and generics parameters) are a little more picky than you might intuitively expect (although that may be more of an issue with numeric autoboxing, not boolean/Boolean).

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points