Why does the Boolean object have a public constructor in Java?

前端 未结 5 1589
逝去的感伤
逝去的感伤 2021-01-03 20:58

Documentation for the constructor new Boolean(boolean value) in Java states:

Note: It is rarely appropriate to use this constructor. Unle

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 21:30

    Usually, you will want to use valueOf(boolean) or even the Boolean.TRUE / Boolean.FALSE constants directly.

    But think of a scenario where you want to use a private Boolean variable as a monitor for synchronizing threads. There you will need to make sure you use your own instance and have full control of it.

提交回复
热议问题