Why const
is a reserved keyword in Java but has no function? If they decided to use final
instead then whats the point of having const
?
Whenever const keyword is attached with any method (), variable, pointer variable, and with the object of a class it prevents that specific object/method ()/variable to modify its data items value. There are a certain set of rules for the declaration and initialization of the constant variables:
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.
TechTarget Contributor Const (constant) in programming is a keyword that defines a variable or pointer as unchangeable. A const may be applied in an object declaration to indicate that the object, unlike a standard variable, does not change. Such fixed values for objects are often termed literals.
Always use constfor constants that might otherwise be defined using a #define or an enum. The compiler can locate the data in read-only memory (ROM) as a result (although the linker is often a better tool for this purpose in embedded systems).
From the JLS:
The keywords
const
andgoto
are reserved, even though they are not currently used. This may allow a Java compiler to produce better error messages if these C++ keywords incorrectly appear in programs.
By way of historical perspective, I can offer you the following quote by Josh Bloch from 2003:
Josh Bloch: We do not have plans to add support for the
const
keyword to the Java language. It was a mixed blessing in C++; as you know, it's merely advisory, and can cast on or off. Gosling hated it, and didfinal
instead. What you really want is "immutable," but it's a research problem to make this work properly.
You would need to ask the pre Java 1.0 designers to find out their original motivation, but I would surmise that they were simply keeping their options open. The goto
reserved word is another example.
My guess is that the text that "aix" found in the JLS is a "postfacto" rationalization. Why? Because struct
, union
, unsigned
and so on are NOT reserved words in Java! In short, I don't buy it ... no matter what the JLS claims :-)
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
"The keywords const and goto are reserved, even though they are not currently used."
i don't know about the exact background but maybe they used to be part of java?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With