When should null values of Boolean be used?

前端 未结 14 2321
心在旅途
心在旅途 2020-12-04 07:32

Java boolean allows values of true and false while Boolean allows true, false, and null. I have

相关标签:
14条回答
  • 2020-12-04 07:59

    I almost never use Boolean because its semantics are vague and obscure. Basically you have 3-state logic: true, false or unknown. Sometimes it is useful to use it when e.g. you gave user a choice between two values and the user didn't answer at all and you really want to know that information (think: NULLable database column).

    I see no reason to convert from boolean to Boolean as it introduces extra memory overhead, NPE possibility and less typing. Typically I use awkward BooleanUtils.isTrue() to make my life a little bit easier with Boolean.

    The only reason for the existence of Boolean is the ability to have collections of Boolean type (generics do not allow boolean, as well as all other primitives).

    0 讨论(0)
  • 2020-12-04 08:00

    I suppose in some case, you should have a mechanism to distinguish a Boolean field which already set value or not.

    0 讨论(0)
提交回复
热议问题