为什么Boolean对象在Java中的公共构造? [英] Why does the Boolean object have a public constructor in Java?

查看:176
本文介绍了为什么Boolean对象在Java中的公共构造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关构造文档新布尔(布尔值)在Java中指出:

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

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

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.

如果是这样,这是为什么构造函数public,而不是去precated?是有史以来有一个很好的理由来使用此构造,而不是通过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.

这张票解释了没有去precating构造函数的原因:

This ticket explains the reasons for not deprecating the constructors:

由于中断德precating的API可以有,目前的API
  必须是积极的危险是德precated,就像使用Thread.stop。
  虽然使用这个构造肯定是不明智的,这不
  上升(或漏极)到危害性的标准是在德precated
  JDK中。在将来,我们会加入诋毁设施,以纪念
  API元素并不那么糟糕,他们应该去precated,
  但不应该在大多数情况下被使用。此构造将是一个很好
  候选人诋毁。

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.

这篇关于为什么Boolean对象在Java中的公共构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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