Why does the Boolean object have a public constructor in Java?(为什么布尔对象在 Java 中有一个公共构造函数?)
问题描述
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.
这篇关于为什么布尔对象在 Java 中有一个公共构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么布尔对象在 Java 中有一个公共构造函数?


- C++ 和 Java 进程之间的共享内存 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01