java - 为什么 Java 中不使用 const 关键字?

标签 java

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:

  1. Adding const is too late now. Had this been added from 1.0, the situation could have been different.

  2. 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++.

  3. 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/

相关文章:

java - WebDriverWait 在 Selenium 4 中已弃用

Java小程序无法通过浏览器访问mysql

Java 版本 : Executing manifest/build/null2120978844 is invalid

java - 为什么 Java 不理解我放在这个多维数组中的对象的类?

java - 开发模式下的 GWT + AppEngine 应用程序在本地网络上不可用

java - 使用 JTextField 的对象调用 actionPerformed

java 双除法与 BigDecimal 轮 half_up half_down

java for 循环与空语句

java - 什么是初始化 block ?

java - String.format 运行时格式化