java - 为什么 boolean 对象在 Java 中有一个公共(public)构造函数?

标签 java boolean

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. It is likely to yield significantly better space and time performance.

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

最佳答案

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

This ticket解释了不弃用构造函数的原因:

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 构造函数会是做一些有用的事情的最佳方式。

关于java - 为什么 boolean 对象在 Java 中有一个公共(public)构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7434084/

相关文章:

c - 为什么C语言中boolean类型写成_Bool?

java - 如何从java检查python是否安装在windows中?

Java - boolean 逻辑 - 无法识别对象

java - 无法确定 : com. packt.cardatabase.domain.Owner 的类型,位于表 : car, 的列 : [org. hibernate.mapping.Column(owner)]

java - 如果可以的话,我可以在不使用java中的包装类的情况下对两个字符串值求和吗?

javascript - 逻辑运算符 ||在 javascript 中,0 代表 Boolean false?

string - boolean 值到 TableView JavaFx 中的字符串

c++ - 如果任何条件为假,如何从 for 循环返回假

java - 以线程安全的方式使用 ExecutorService 和 ProgressMonitor

java - 驱动器已满或空间不足的 IOException