When should null values of Boolean be used?

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

    In a strict definition of a boolean element, there are only two values. In a perfect world, that would be true. In the real world, the element may be missing or unknown. Typically, this involves user input. In a screen based system, it could be forced by an edit. In a batch world using either a database or XML input, the element could easily be missing.

    So, in the non-perfect world we live in, the Boolean object is great in that it can represent the missing or unknown state as null. After all, computers just model the real world an should account for all possible states and handle them with throwing exceptions (mostly since there are use cases where throwing the exception would be the correct response).

    In my case, the Boolean object was the perfect answer since the input XML sometimes had the element missing and I could still get a value, assign it to a Boolean and then check for a null before trying to use a true or false test with it.

    Just my 2 cents.

    0 讨论(0)
  • 2020-12-04 07:50

    Wrapper classes for primitives can be used where objects are required, collections are a good sample.

    Imagine you need for some reason store a sequence of boolean in an ArrayList, this can be done by boxing boolean in Boolean.

    There is a few words about this here

    From documentation:

    As any Java programmer knows, you can’t put an int (or other primitive value) into a collection. Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class (which is Integer in the case of int). When you take the object out of the collection, you get the Integer that you put in; if you need an int, you must unbox the Integer using the intValue method. All of this boxing and unboxing is a pain, and clutters up your code. The autoboxing and unboxing feature automates the process, eliminating the pain and the clutter.

    http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

    0 讨论(0)
  • 2020-12-04 07:51

    There are three quick reasons:

    • to represent Database boolean values, which may be true, false or null
    • to represent XML Schema's xsd:boolean values declared with xsd:nillable="true"
    • to be able to use generic types: List<Boolean> - you can't use List<boolean>
    0 讨论(0)
  • 2020-12-04 07:53

    Boolean wrapper is useful when you want to whether value was assigned or not apart from true and false. It has the following three states:

    • True
    • False
    • Not defined which is null

    Whereas boolean has only two states:

    • True
    • False

    The above difference will make it helpful in Lists of Boolean values, which can have True, False or Null.

    0 讨论(0)
  • 2020-12-04 07:53

    Boolean can be very helpful when you need three state. Like in software testing if Test is passed send true , if failed send false and if test case interrupted send null which will denote test case not executed .

    0 讨论(0)
  • 2020-12-04 07:56

    Use boolean rather than Boolean every time you can. This will avoid many NullPointerExceptions and make your code more robust.

    Boolean is useful, for example

    • to store booleans in a collection (List, Map, etc.)
    • to represent a nullable boolean (coming from a nullable boolean column in a database, for example). The null value might mean "we don't know if it's true or false" in this context.
    • each time a method needs an Object as argument, and you need to pass a boolean value. For example, when using reflection or methods like MessageFormat.format().
    0 讨论(0)
提交回复
热议问题