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/