When should null values of Boolean be used?

前端 未结 14 2319
心在旅途
心在旅途 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:39

    There are many uses for the **null** value in the Boolean wrapper! :)

    For example, you may have in a form a field named "newsletter" that indicate if the user want or doesn't want a newsletter from your site. If the user doesn't select a value in this field, you may want to implement a default behaviour to that situation (send? don't send?, question again?, etc) . Clearly, not set (or not selected or **null**), is not the same that true or false.

    But, if "not set" doesn't apply to your model, don't change the boolean primitive ;)

提交回复
热议问题