Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java core: returning null as boolean

Tags:

java

null

boolean

I noticed this thing: my class has boolean member and at some run-point I see it's value in debugger as null. But when I use the getter method, it returns false.

I know, most books tell you that boolean in Java has only two values, unlike C/C++ in which every non-zero number can be interpreted as true. So I wonder, what's happening in there?

Are there any other situations when some value of other type or null can be interpreted as false? Perhaps explanation of this is somewhere in JLS, could someone point me to it?

EDIT: Indeed yesterday was a bad day for debugging. First i wasted 4 hours to find a source of a bug which disapeared when i reversed source and made same changes again. Then this. I just tried to reproduce and couldn't. Was it galucination or it's just monday is a hard day? I don't know. Thanks everyone for your responses! I guess you can vote to close or delete this question now.

like image 387
Aleksandr Kravets Avatar asked Oct 29 '12 16:10

Aleksandr Kravets


1 Answers

It sounds like the field is declared as the nullable, wrapper type Boolean instead of a boolean primitive.

like image 99
Matt Ball Avatar answered Oct 15 '22 21:10

Matt Ball