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

前端 未结 5 1592
逝去的感伤
逝去的感伤 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:23

    valueOf() only got added in Java 1.4, so it would appear that the constructors exist for backwards compatibility.

    This ticket explains the reasons for not deprecating the constructors:

    Due to the disruption deprecating an API can have, currently an API has to be "actively hazardous" to be deprecated, like Thread.stop. While the use this constructor is certainly ill-advised, it doesn't rise (or sink) to the standard of hazardousness to be deprecated in the JDK. In the future we may add a "denigration" facility to mark API elements that aren't quite so bad that they should be deprecated, but shouldn't be used in most cases. This constructor would be a good candidate for denigration.

    I can't think of a realistic scenario where using Boolean constructors would be the best way to do something useful.

提交回复
热议问题