Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

whats the purpose of "const" keyword?

Tags:

java

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?

like image 338
flav Avatar asked May 31 '12 07:05

flav


People also ask

What is the use of const keyword in C++?

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:

Why is the keyword const reserved in the Java language?

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.

What does const mean in C++?

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.

When should I use const for constants?

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).


3 Answers

From the JLS:

The keywords const and goto 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 did final instead. What you really want is "immutable," but it's a research problem to make this work properly.

like image 116
NPE Avatar answered Oct 12 '22 12:10

NPE


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 :-)

like image 31
Stephen C Avatar answered Oct 12 '22 12:10

Stephen C


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?

like image 39
tagtraeumer Avatar answered Oct 12 '22 13:10

tagtraeumer