简体   繁体   English

为什么 Boolean 对象在 Java 中有一个公共构造函数?

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

Documentation for the constructor new Boolean(boolean value) in Java states: Java 中构造函数new Boolean(boolean value)文档说明:

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.除非需要新实例,否则静态工厂valueOf(boolean)通常是更好的选择。 It is likely to yield significantly better space and time performance.它可能会产生明显更好的空间和时间性能。

If so, why is this constructor public and not deprecated?如果是这样,为什么这个构造函数是公开的而不是被弃用的? Is there ever a good reason to use this constructor instead of Boolean.valueOf() ?是否有充分的理由使用此构造函数而不是Boolean.valueOf()

valueOf() only got added in Java 1.4, so it would appear that the constructors exist for backwards compatibility. valueOf()仅在 Java 1.4 中添加,因此构造函数的存在似乎是为了向后兼容。

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.由于弃用 API 可能会造成中断,因此当前 API 必须具有“积极危险”才能弃用,例如 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.虽然使用这个构造函数肯定是不明智的,但它并没有上升(或下降)到 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.将来,我们可能会添加一个“贬低”工具来标记 API 元素,这些元素不是很糟糕,应该被弃用,但在大多数情况下不应该使用。 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.我想不出一个现实的场景,其中使用Boolean构造函数是做一些有用的事情的最佳方式。

Usually, you will want to use valueOf(boolean) or even the Boolean.TRUE / Boolean.FALSE constants directly.通常,您会想要直接使用valueOf(boolean)甚至Boolean.TRUE / Boolean.FALSE常量。

But think of a scenario where you want to use a private Boolean variable as a monitor for synchronizing threads.但是想一想您想要使用私有Boolean变量作为同步线程的监视器的场景。 There you will need to make sure you use your own instance and have full control of it.在那里,您需要确保使用自己的实例并完全控制它。

另一个不一定很好的理由可能是简单地保持它与其他本机包装器一致。

As of Java 9, the Boolean(boolean) constructor has been deprecated ;从 Java 9 开始, Boolean(boolean)构造函数已被弃用 see javadoc .请参阅javadoc

For those who care about the history, there was a longstanding bug that called for the deprecation of the constructor.对于那些关心历史的人来说,有一个长期存在的错误要求弃用构造函数。 It was formally proposed in JEP 277 along with a number of other deprecations.它是在JEP 277 中正式提出的,还有一些其他的弃用。

The reason it hasn't been deprecated is that Java maintains backwards compatibility to version 1.0它没有被弃用的原因是 Java 保持向后兼容 1.0 版

I can't think of a good reason to use the constructor.我想不出使用构造函数的好理由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM