gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:34:10 25 4
gpt4 key购买 nike

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. Inaddition to creeping featurism, we see the following problemswith 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 methodsto be marked with a const keyword. This means that mostmethods will have to be marked const explicitly. This tendto clutter all methods in C++.

  3. Compatibility is a very important feature of the JDK.Arguably, the collection classes should be modified toindicate that the elements are const. That wouldrequire all existing implementations to be updated inthe same way, effectively breaking all existing non-JDKimplementations of the collection interfaces. Similarly,hashCode would have to be const, breaking the currentimplementation of String."


更新

出于好奇,我花了几分钟浏览 Project COIN mailing list 的主题行。 .令我惊讶的是,没有人愿意建议const。 (或者如果他们这样做了,我错过了。)

所以要么没有人(足够)关心这个想法,要么有足够专业知识来制定项目 COIN 提案的人认识到它不可能通过集合。

关于java - 为什么 Java 中不使用 const 关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7428358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com