I was trying to identify the reason behind constants in Java
I have learned that Java allows us to declare constants by using final keyword.
My question is why didn’t Java introduce a Constant (const) feature. Since many people say it has come from C++, in C++ we have const keyword.
Please share your thoughts.
Every time I go from heavy C++ coding to Java, it takes me a little while to adapt to the lack of const-correctness in Java. This usage of
constin C++ is much different than just declaring constant variables, if you didn’t know. Essentially, it ensures that an object is immutable when accessed through a special kind of pointer called a const-pointer When in Java, in places where I’d normally want to return a const-pointer, I instead return a reference with an interface type containing only methods that shouldn’t have side effects. Unfortunately, this isn’t enforced by the langauge.Wikipedia offers the following information on the subject: