What is the difference between Boolean.TRUE and true in Java?

后端 未结 7 1189
情歌与酒
情歌与酒 2020-12-15 03:02

PS: I understand the difference between \"true\" and true.

Edit: I also understand that Boolean.TRUE is a wrapper for the primitive true, my question then is - why d

相关标签:
7条回答
  • 2020-12-15 03:42

    Boolean.TRUE is a reference to an object of the class Boolean, while true is just a value of the primitive boolean type. Classes like Boolean are often called "wrapper classes", and are used when you need an object instead of a primitive type (for example, if you're storing it in a data structure).

    0 讨论(0)
  • 2020-12-15 03:45

    Boolean.TRUE is a wrapper object and singleton . true is a literal constant. Below are 2 situations where I use wrappers over primitives

    1. I want to store them in Collections
    2. I'd want to have a notion of null. primitive boolean can only represent two states.
    0 讨论(0)
  • 2020-12-15 03:49

    The reason

    boolean boolVar = Boolean.TRUE;
    

    works is because of autounboxing, a Java 5 feature that allows a wrapper object to be converted to its primitive equivalent automatically when needed. The opposite, autoboxing, is also possible:

    Boolean boolVar = true;
    
    0 讨论(0)
  • 2020-12-15 03:53

    true is of the primitive boolean type while Boolean.TRUE is a Boolean object that wraps the true value.

    0 讨论(0)
  • 2020-12-15 03:57

    Primitive types, (e.i. boolean), are strongly preferred over classes (e.i. Boolean) for a number of reasons. See discussion here. https://softwareengineering.stackexchange.com/questions/203970/when-to-use-primitive-vs-class-in-java. Primitive type makes code more readable, prevents pointer errors, such as if(a==b) vs if(a.equals(b)), increases performance and follows the conversion.

    There is one case when Boolean or Integer works better than boolean and int. That is if you have a situation when you want to allow null as a value. This results in a number of null checks, but it prevents false from slipping through when

    0 讨论(0)
  • 2020-12-15 04:04

    As the previous answers stated, Boolean.TRUE returns the wrapper object of the boolean value true, so for the contexts where we need to treat boolean's like objects (for example, having an ArrayList of booleans), we could use Boolean.TRUE or Boolean.FALSE

    As for why:

    boolean boolVar = Boolean.TRUE;
    

    is valid is because of Autoboxing and Unboxing.

    In short, the Java compiler, when it sees you treating a primitive like an object, such as

    List<Boolean> listOfBoolean = new ArrayList<Boolean>();
    boolean someBool = true;
    listOfBoolean.add(someBool);
    

    it will automatically wrap it, or autobox it

    List<Boolean> listOfBoolean = new ArrayList<Boolean>();
    boolean someBool = true;
    listOfBoolean.add(Boolean.valueOf(someBool));
    

    And if it sees you treating a wrapper object, like Boolean.TRUE, as a primitive, like:

    boolean boolVar = Boolean.TRUE;
    

    it will convert it down into a primitive, or unbox it, like if we did:

    boolean boolVar = Boolean.TRUE.booleanValue();
    

    Once upon a time, you'd have to do this by hand, but now, for better or for worse, this is mostly taken care of for you.

    And if you're wondering why have Boolean.TRUE at all, it's because there is no need to have floating around lots of Boolean objects for true. Since a boolean can only be one of two values, it's simpler just to have them as constants rather than, for every time someone needs boxed up true:

    Boolean trueBool = new Boolean(true); 
    
    0 讨论(0)
提交回复
热议问题