问题描述
java 中构造函数 new boolean(boolean value) 的文档说明:
documentation for the constructor new boolean(boolean value) in java states:
注意:很少使用这个构造函数.除非需要新实例,否则静态工厂 valueof(boolean) 通常是更好的选择.它可能会产生明显更好的空间和时间性能.
note: it is rarely appropriate to use this constructor. unless a new instance is required, the static factory valueof(boolean) is generally a better choice. it is likely to yield significantly better space and time performance.
如果是这样,为什么这个构造函数是公开的而不被弃用?是否有充分的理由使用此构造函数而不是 boolean.valueof()?
if so, why is this constructor public and not deprecated? is there ever a good reason to use this constructor instead of boolean.valueof()?
推荐答案
valueof() 仅在 java 1.4 中添加,因此构造函数的存在似乎是为了向后兼容.
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:
由于弃用 api 可能造成的中断,目前的 api必须是积极危险"才能被弃用,例如 thread.stop.虽然使用这个构造函数肯定是不明智的,但它并没有上升(或下降)到要弃用的危险性标准jdk.将来我们可能会添加一个诋毁"设施来标记api 元素还不错,应该被弃用,但在大多数情况下不应使用.这个构造函数会很好诋毁候选人.
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.
我想不出一个现实的场景,使用 boolean 构造函数会是做一些有用的事情的最佳方式.
i can't think of a realistic scenario where using boolean constructors would be the best way to do something useful.