Search code examples
javasyntaxkeywordgoto

Is there a goto statement in Java?


I'm confused about this. Most of us have been told that there isn't any goto statement in Java.

But I found that it is one of the keywords in Java. Where can it be used? If it can not be used, then why was it included in Java as a keyword?


Solution

  • The Java keyword list specifies the goto keyword, but it is marked as "not used".

    It was in the original JVM (see answer by @VitaliiFedorenko), but then removed. It was probably kept as a reserved keyword in case it were to be added to a later version of Java.

    If goto was not on the list, and it gets added to the language later on, existing code that used the word goto as an identifier (variable name, method name, etc...) would break. But because goto is a keyword, such code will not even compile in the present, and it remains possible to make it actually do something later on, without breaking existing code.