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

后端 未结 7 1194
情歌与酒
情歌与酒 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: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

提交回复
热议问题