3

Does the following expression evaluate to true or false in Java?

null == false

I can't find any information about this on the internet.

Note that this question is about wrapped Boolean value, and not about primitive boolean/true/false.

Community
  • 1
  • 1
Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87
  • 11
    Not to be rude, but have you tried evaluating it to find out? – Wai Ha Lee May 14 '15 at 09:51
  • 1
    You can't compare `null` to a `boolean`. They are different types one indicating a *null reference pointer* and the other a false/not true value. Thus the answer is: No this expression is not even valid (and if it was, it would be false). – GiantTree May 14 '15 at 09:53
  • 2
    @WaiHaLee It will be much easier for people to find out the answer by searching google and finding this, rather than starting up their IDE and creating new project just to find the answer to this simple question. – Babken Vardanyan May 14 '15 at 09:55
  • Why fire an IDE at all? Write a simple program by hand and try and compile it (hint: it doesn't) – fge May 14 '15 at 09:55
  • 1
    @GiantTree Ok, write that as an answer and I'll accept it. – Babken Vardanyan May 14 '15 at 09:56
  • 2
    It is so easy to find the answer by trying. Why bother searching on the Internet? – Hakan May 14 '15 at 10:06

4 Answers4

4

You can't compare null to a boolean.
They are different types one indicating a null reference pointer and the other one a false/not true value.

Thus the answer is: No this expression is not even valid and if it was, it would be false.

GiantTree
  • 218
  • 3
  • 13
1

The null key word presents a pointer which point... nowhere. Which means Java can't get the value in the memory by it's index.

A boolean is a memory value of 1 bit, 0 is false and 1 is true (or inverse). The value is get with the pointing index of the variable (if this boolean is stock in such a variable)

You can't check an equality between something representing nothing and a one bit value. Your line will throw an error !

Before ask, you should try this out yourself !

Kapcash
  • 6,377
  • 2
  • 18
  • 40
0

null and false is not comparable

Lauris01
  • 283
  • 1
  • 4
  • 19
0

it gives The operator == is undefined for the argument type(s) null, boolean error

you can compare false == false or null == null;

Chickenturtle
  • 159
  • 11