How to write a constructor that contains a boolean value?

前端 未结 3 1724
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 05:24

This is a dumb question but it\'s been a long time since I\'ve worked with java... How can I write my constructor with Boolean values or should I just write a default construct

3条回答
  •  一个人的身影
    2021-01-27 05:58

    You probably appropriately chose the primitive type boolean in your example but since you mentioned "Boolean" with a capital "B", there is a difference. Boolean variables are nullable unlike the primitive "boolean" with a lower case "b". If you had used Booleans, you can simply initialize those like

    Boolean boolVar = Boolean.TRUE; //or Boolean.FALSE
    

    And they might later require null checks depending on the situation.

提交回复
热议问题