Why are const and goto not used in java?

Question:

Let's talk about two reserved but unused keywords in the Java language – and . Why are they not being used? As I understand it, can be replaced with public static final , but what about ?

Answer:

Update

The Java specification ( §3.9. Keywords ) provides a reason for reserving const and goto , not related to future plans:

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.
The const and goto keywords are reserved, although not currently used. This will allow the Java compiler to output clearer error messages if these C ++ keywords are misused in programs.

That. Java developers initially had no plans to use these words, but reserved them to catch errors when copying code written in C ++ (if the variables could be called goto or const , then the copied C ++ code could be compiled by mistake and lead to unexpected results during execution ). This portion of the specification has not changed since the first edition of the JLS.

Below is an initial version of the answer that looks at trying to find a use for const and goto in Java.

Many letters: const and goto from C ++. The keywords have no use in Java. The functions they perform in C ++ are implemented differently in Java.

Story

Historically, Java syntax is based on C ++, so the developers decided to reserve a number of C ++ keywords to:

  • catch errors when copying code written in C ++;
  • use them in the future when expanding the language;
  • Simplify the transition to Java for C ++ developers.

const and goto

Thus, the question boils down to the following: "Does Java need const and goto and what should they do?"

const

One of the proposals to introduce const by analogy with C ++ can be viewed at the link:

JDK-4211070: Java should support const parameters (like C ++) for code maintainence

The offer was closed in 2005. Here's a breakdown of the parsing, which notes issues related to const input:

  • Adding const is too late now. Had this been added from 1.0, the situation could have been different.

  • Const pollution: the C ++ approach requires all const methods to be marked with a const keyword. This means that most methods will have to be marked const explicitly. This tend to clutter all methods in C ++.

  • Compatibility is a very important feature of the JDK. Arguably, the collection classes should be modified to indicate that the elements are const. That would require all existing implementations to be updated in the same way, effectively breaking all existing non-JDK implementations of the collection interfaces. Similarly,
    hashCode would have to be const, breaking the current implementation of String.

The creation of constant variables / fields in Java is already provided with final . The introduction of mechanisms from C ++ was considered unjustified.

goto

goto bad reputation (see Dijkstra's " Go To Statement Considered Harmful "). Java has alternative transition operators: break , continue , return , which cover most of the scope. The only reason for goto is that in some cases, using goto can simplify your code. Apparently the goto proponents were unable to exert enough influence on the developers to add a new keyword.

Similar discussions in the English version:

Scroll to Top