Why are const and goto not used in java?


Let's talk about two reserved but unused Java keywords - const and goto. Why aren't they used? As I understand it, const can be replaced with public static final, but what about goto?

3 answers

Update

The Java specification (§3.9. Keywords) specifies the reason for reserving const and goto, which is 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 keywords const and goto are reserved, although they are not currently used. allows the Java compiler to output clearer error messages if these C++ keywords are used incorrectly in programs.

Thus, the 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 mistakenly compiled and lead to unexpected results during execution). This part of the specification has not changed since the first edition of JLS.

Below is the original version of the answer, which discusses attempts to find a use for const and goto in Java.

Many bukav: const and goto moved from C++. Keywords are not used in Java. The functions they perform in C++ are implemented differently in Java.

History

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

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

const and goto

Thus, the question boils down to the following: "Is there a need in Java for const and goto and what should they do?"

Const

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

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

The offer was closed in 2005. Follow the link to view the analysis, in the outputs of which the problems associated with entering const are noted:

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

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

Goto

goto has a bad reputation (see Dijkstra's " Go To Statement Considered Harmful"). Java has alternative jump operators: break, continue, return, which cover most of the application area. The only argument in favor of goto is that in some cases, using goto allows you to simplify the code. Apparently, the goto supporters were not able to exert enough influence on the developers to add a new keyword.

Similar discussions in the English version:

 19
Author: default locale, 2018-03-20 06:45:35

goto it is considered a "bad operator", impairs readability and increases code obfuscation, generates hard-to-maintain "spaghetti code" (also violates very important programming concepts, you can read the link at the end of the answer).

Some serious people believe that it has no place in programming languages, and write whole books about it. So, while someone is talking about it, the creators of Java are doing it. They sincerely believe that without an unconditional transition, the world will become a little kinder, and on the totality of problems and advantages, you can agree with them. Learn more

The word itself was reserved at the stage of the creation of the language, having migrated from other popular ones at that time. It's still hanging around...

PS: In Java, there are still unconditional transitions implemented via the break and continue operators with labels. How often did you use them and did you know about this feature at all (this is about the need for this operator)?

 13
Author: pavlofff, 2017-05-19 06:52:45

For example, they can not use, but be in the language, as a reserve for the future, so that in subsequent versions you can easily add this operator and not intersect with the name of variables/objects in the code already written(Legacy) in the old version of the language.

In JavaScript, it seems, the same practice.

Regarding const, which can be replaced by public static final:

Nothing prevents JAVA developers from implementing const in the future and adding so-called "syntactic sugar" when you can do both, and in the end, the program will run the same way.

For example, C# has a lot of syntactic sugar:

  1. You can declare variables by alias (int) or by the type itself(Int32)
  2. There is LINQ, which allows you to do without loops in simple cases , etc...
 5
Author: iluxa1810, 2017-05-20 08:03:57