views:

472

answers:

6

I was trying to identify the reason behind the "CONSTANTS" in Java I have learnt that Java allows us to declare constants by using final keyword.

My question is why didn't Java introduce Constant (const) keyword. Since many people say it has come from C++, in C++ we have const keyword.

Please share your thoughts.

+4  A: 

Const isn't used because you can't enforce the fact that a value is constant in Java. It would therefore be something of a misnomer. Final is arguably (though subjectively) a better name, as it indicates that the the variable is the final reference (it cannot be reassigned).

As an aside, const is a reserved but unused keyword in Java.

GaryF
+2  A: 

There would be two ways to define constants - const and static final, with the exact same semantics. Furthermore static final describes the behaviour better than const

Bozho
@Bozho, you said better behaviour than Const, what way it is? can you share any example
harigm
well, the variable is `static` (not belonging to particular instance) and `final` - cannot be changed.
Bozho
+1  A: 

The C++ semantics of const are very different to Java final. If the designers had used const it would have been unnecessarily confusing.

The fact that const is a reserved word suggests the designers had ideas of implementing const, but they have since decided against it; see this closed bug. The stated reasons include that adding support for C++ style const would cause compatibility problems.

Stephen C
+1  A: 

const in C++ does not mean that a value is a constant.

const in C++ implies that the client of a contract undertakes not to alter its value.

Whether the value of a const expression changes becomes more evident if you are in an environment which supports thread based concurrency.

As Java was designed from the start to support thread and lock concurrency, it didn't add to confusion by overloading the term to have the semantics that final has.

eg:

#include <iostream>

int main ()
{
    volatile const int x = 42;

    std::cout << x << std::endl;

    *const_cast<int*>(&x) = 7;

    std::cout << x << std::endl;

    return 0;
}

outputs 42 then 7.

Although x marked as const, as a non-const alias is created, x is not a constant. Not every compiler requires volatile for this behaviour (though every compiler is permitted to inline the constant)

With more complicated systems you get const/non-const aliases without use of const_cast, so getting into the habit of thinking that const means something won't change becomes more and more dangerous. const merely means that your code can't change it without a cast, not that the value is constant.

Pete Kirkham
const int x = 42; - x is a constant
anon
@Neil I think he's talking about objects
Andy Johnson
@andy Works the same for objects.
anon
@Neil What I meant was: when Pete wrote that const does not mean a value is constant, my understanding was that he was only talking about primitive types. To explain further (and I appreciate that you know this stuff): For a const non-object variable (say an int), the compiler enforces the contract. If I declare it const then it can't be changed. For a object I have to declare as const any member functions that modify the object's internal state. If I don't then its possible to have a const instance of that class that is (internally) mutable. That was my understanding of what he wrote.
Andy Johnson
@Neil If you have one object or variable aliased by both const and non-const pointers, then the value of the const alias can be changed by the non-const alias. Therefore `const` does not mean a value is constant. It means that the client of a value is constrained not to mutate it.In your example there is no alias, so all users are under the same constraint. This is not the case in general. `const` effects the clients, not the value - it says you can't change it, not that it won't change.
Pete Kirkham
I was thinking about the objects
harigm
+8  A: 

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 const in 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:

Interestingly, the Java language specification regards const as a reserved keyword — i.e., one that cannot be used as variable identifier — but assigns no semantics to it. It is thought that the reservation of the keyword occurred to allow for an extension of the Java language to include C++-style const methods and pointer to const type. The enhancement request ticket in the Java Community Process for implementing const correctness in Java was closed in 2005, implying that const correctness will probably never find its way into the official Java specification.

Gunslinger47
+2  A: 

What does const mean
First, realize that the semantics of a "const" keyword means different things to different people:

  • read-only reference - Java final semantics - reference variable itself cannot be reassigned to point to another instance (memory location), but the instance itself is modifiable
  • readable-only reference - C const pointer/reference semantics - means this reference cannot be used to modify the instance (e.g. cannot assign to instance variables, cannot invoke mutable methods) - affects the reference variable only, so a non-const reference pointing to the same instance could modify the instance
  • immutable object - means the instance itself cannot be modified - applies to instance, so any non-const reference would not be allowed or could not be used to modify the instance
  • some combination of the the above?
  • others?

Why or Why Not const
Second, if you really want to dig into some of the "pro" vs "con" arguments, see the discussion under this request for enhancement (RFE) "bug". This RFE requests a "readable-only reference"-type "const" feature. Opened in 1999 and then closed/rejected by Sun in 2005, the "const" topic was vigorously debated:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4211070

While there are a lot of good arguments on both sides, some of the oft-cited (but not necessarily compelling or clear-cut) reasons against const include:

  • may have confusing semantics that may be misused and/or abused (see the What does const mean above)
  • may duplicate capability otherwise available (e.g. designing an immutable class, using an immutable interface)
  • may be feature creep, leading to a need for other semantic changes such as support for passing objects by value

Before anyone tries to debate me about whether these are good or bad reasons, note that these are not my reasons. They are simply the "gist" of some of the reasons I gleaned from skimming the RFE discussion. I don't necessarily agree with them myself - I'm simply trying to cite why some people (not me) may feel a const keyword may not be a good idea. Personally, I'd love more "const" semantics to be introduced to the language in an unambiguous manner.

Bert F