为什么布尔对象在 Java 中有一个公共构造函数? [英] Why does the Boolean object have a public constructor in Java?

查看:24
本文介绍了为什么布尔对象在 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 中有一个公共构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆