问题描述
为什么在Java中没有使用const关键字?
你能否看到使用某些关键字或为什么,Sun是否提供任何解释?
Can you see reasons for closing the request, does Sun provides any explanations?
推荐答案
是。 Sun提供了3个理由说明他们为什么不对。我引用:
Yes. Sun provided 3 reasons for why they won't act on the request in the request itself. I quote:
-
现在添加const太晚了。这是从1.0添加的,
情况可能有所不同。
Adding const is too late now. Had this been added from 1.0, the situation could have been different.
Const污染:C ++方法要求所有const方法
都用const关键字标记这意味着大多数
方法必须明确地标记为const。这会使
混乱C ++中的所有方法。
Const pollution: the C++ approach requires all const methods to be marked with a const keyword. This means that most methods will have to be marked const explicitly. This tend to clutter all methods in C++.
兼容性这是JDK的一个非常重要的特性。
可以说,集合类应该修改为
表示元素是const。那就是
要求所有现有的实现都在$ b $中更新b以同样的方式,有效地破坏了集合接口的所有现有的非JDK
实现。同样,
hashC ode必须是const,打破String的当前
实现。
Compatibility is a very important feature of the JDK. Arguably, the collection classes should be modified to indicate that the elements are const. That would require all existing implementations to be updated in the same way, effectively breaking all existing non-JDK implementations of the collection interfaces. Similarly, hashCode would have to be const, breaking the current implementation of String."
更新
出于好奇,我花了几分钟时间拖网的主题行。令我惊讶的是,没有人打算建议 const
。 (或者,如果他们这样做了,我就错过了。)
Out of curiousity, I spent a few minutes trawling through the subject lines of the Project COIN mailing list. Somewhat to my surprise, nobody bothered to suggest const
. (Or if they did, I missed it.)
因此,任何人都不再关心这个想法,或者有足够专业知识来制定项目COIN提案的人认识到它没有机会通过集合。
So either nobody cares (enough) for this idea any more, or people with sufficient expertise to formulate a project COIN proposal recognize that there's no chance that it would pass muster.
这篇关于为什么const关键字没有在Java中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!