What is the purpose of the const keyword?

Why is const a reserved keyword in Java but has no function? If they decide to use final instead, then what's the point of having const ?

+6
source share
6 answers

From JLS :

The const and goto keywords are reserved, even if they are not currently in use. This may allow the Java compiler to generate improved error messages if these C ++ keywords do not display correctly in programs.

As a historical perspective, I can offer you the following 2003 Josh Bloch quote :

Josh Bloch: We have no plans to add support for the const keyword in the Java language. It was a mixed blessing in C ++; as you know, this is just a consultation and can turn it on or off. Gosling hated him and instead made final . What you really want is β€œunchanging,” but it is a research problem to do this job properly.

+9
source

You need to ask pre Java 1.0 designers to find out their original motivation, but I would suggest that they just keep their capabilities open. Another example is the reserved word goto .

My guess is that the text that "aix" found in JLS is a "post fact" rationalization. What for? Since struct , union , unsigned etc. NOT reserved words in Java! In short, I don't buy it ... no matter what the JLS claims :-)

+3
source

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

"The const and goto keywords are reserved, even if they are not currently in use."

I don't know about the exact background, but maybe they were part of java?

+2
source

This is only a reserved keyword . It has no use or functionality.

+1
source

It is believed that keyword reservation occurred to allow the Java language extension to include const and C ++ methods in the const type. An upgrade request ticket in the Java Community Process for implementing const correctness in Java was closed in 2005, implying that const correctness is unlikely to find its way into the official Java specification.

0
source

From JLS -

"The const and goto keywords are reserved even if they are not currently in use. This may allow the Java compiler to generate more efficient error messages if these C ++ keywords do not display correctly in programs."

0
source

Source: https://habr.com/ru/post/917078/


All Articles