6

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.

5
  • Are you sure that it's not a Boolean field? Commented Oct 29, 2012 at 16:16
  • Please show the code of this class. At least the field declaration and the getter method. Commented Oct 29, 2012 at 16:22
  • Can you show the relevant parts of your code? (boolean declaration, getter, the point where you see it null) And let us know what debugger you are using. Commented Oct 29, 2012 at 16:23
  • 1
    It is impossible. Show your code. I believe that somewhere you are using Boolean instead of boolean. Commented Oct 29, 2012 at 16:25
  • ok i will try to reproduce it in clean environment tomorrow and will post the code if it confirms Commented Oct 29, 2012 at 21:10

1 Answer 1

12

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

Sign up to request clarification or add additional context in comments.

10 Comments

In this case, the getter would not return false, but throw a NPE.
@rolve unless it does something like return boolField != null && boolField
A getter will throw an NPE if it returns a boolean, but not if it returns a Boolean.
I think all we can really say is that we need to see the code first.
Ok, I'll try to reproduce outside the project tomorrow.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.