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

发布于 2024-12-05 02:25:50 字数 276 浏览 1 评论 0原文

Java 中构造函数 new Boolean(boolean value) 的文档指出:

注意:很少适合使用此构造函数。除非需要新实例,否则静态工厂 valueOf(boolean) 通常是更好的选择。它可能会产生明显更好的空间和时间性能。

如果是这样,为什么这个构造函数是公开的并且没有被弃用?是否有充分的理由使用此构造函数而不是 Boolean.valueOf()

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

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.

If so, why is this constructor public and not deprecated? Is there ever a good reason to use this constructor instead of Boolean.valueOf()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

睫毛溺水了 2024-12-12 02:25:50

valueOf() 仅在 Java 1.4 中添加,因此构造函数的存在似乎是为了向后兼容。

这张票解释了不弃用构造函数的原因:

由于弃用 API 可能会造成破坏,目前 API
必须“非常危险”才能被弃用,例如 Thread.stop。
虽然使用这个构造函数肯定是不明智的,但它并不
上升(或下降)到危险标准,并在
JDK。将来我们可能会添加“诽谤”设施来标记
API 元素还没有那么糟糕以至于应该被弃用,
但在大多数情况下不应该使用。这个构造函数将是一个很好的
诽谤的候选人。

我无法想象使用布尔构造函数是做一些有用的事情的最佳方式的现实场景。

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.

桃扇骨 2024-12-12 02:25:50

通常,您需要直接使用 valueOf(boolean) 甚至 Boolean.TRUE / Boolean.FALSE 常量。

但请考虑一个场景,您希望使用私有布尔变量作为同步线程的监视器。在那里,您需要确保使用自己的实例并完全控制它。

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.

自演自醉 2024-12-12 02:25:50

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

Another, not necessarily good reason would probably be to simply keep it consistent with the other native wrappers.

゛时过境迁 2024-12-12 02:25:50

从 Java 9 开始,Boolean(boolean) 构造函数已被弃用;请参阅 javadoc

对于那些关心历史的人来说,有一个长期存在的 bug要求弃用构造函数。它是在 JEP 277 中正式提出的,同时还有许多其他弃用。

As of Java 9, the Boolean(boolean) constructor has been deprecated; see 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.

可爱暴击 2024-12-12 02:25:50

它没有被弃用的原因是 Java 保持了对 1.0 版本的向后兼容性,

我想不出使用构造函数的好理由。

The reason it hasn't been deprecated is that Java maintains backwards compatibility to version 1.0

I can't think of a good reason to use the constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文