Possible Duplicate:
Why is there no Constant keyword in Java?
为什么Java中不使用const关键字?
你能看出使用一些 transitive const or immutable 的任何缺点吗? Java 语法中的关键字或为什么 common cumbersome approach被选中了?
您能看到关闭request 的原因吗? ,Sun 有解释吗?
最佳答案
Can you see reasons for closing the request, does Sun provides any explanations?
是的。 Sun 在 request itself 中提供了他们不会对请求采取行动的 3 个原因。 .我引用:
"There are no current plans to add this feature to Java. In addition to creeping featurism, we see the following problems with this feature:
Adding const is too late now. Had this been added from 1.0, the situation could have been different.
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++.
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."
更新
出于好奇,我花了几分钟浏览 Project COIN mailing list 的主题行。 .令我惊讶的是,没有人愿意建议const
。 (或者如果他们这样做了,我错过了。)
所以要么没有人(足够)关心这个想法,要么有足够专业知识来制定项目 COIN 提案的人认识到它不可能通过集合。
关于java - 为什么 Java 中不使用 const 关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7428358/