I'm confused about this. Most of us have been told that there is no goto statement in Java. But I found that it is one of the keyword in Java. Where can it be used? If it can not be used, then why was it included in Java as a keyword?
No, thankfully, there isn't goto
in Java.
The goto
keyword is only reserved, but not used. (same goes for const
)
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html
"The keywords const and goto are reserved, even though they are not currently used. "
The Java keyword list specifies the goto
keyword, but it is marked as "not used".
This was probably done in case it were to be added to a later version of Java.
If goto
weren't on the list, and it were added to the language later on, existing code that used the word goto
as an identifier (variable name, method name, etcetera) 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.
No, goto
is not used in Java, despite being a reserved word. The same is true for const
. Both of these are used in C++, which is probably the reason why they're reserved; the intention was probably to avoid confusing C++ programmers migrating to Java, and perhaps also to keep the option of using them in later revisions of Java.
They are reserved for future use (see: Java Language Keywords)
The keywords const and goto are reserved, even though they are not currently used.
The reason why there is no goto statement in Java can be found in "The Java Language Environment":
Java has no goto statement. Studies illustrated that goto is (mis)used more often than not simply "because it's there". Eliminating goto led to a simplification of the language--there are no rules about the effects of a goto into the middle of a for statement, for example. Studies on approximately 100,000 lines of C code determined that roughly 90 percent of the goto statements were used purely to obtain the effect of breaking out of nested loops. As mentioned above, multi-level break and continue remove most of the need for goto statements.
As was pointed out, there is no goto
in Java, but the keyword was reserved in case Sun felt like adding goto
to Java one day. They wanted to be able to add it without breaking too much code, so they reserved the keyword. Note that with Java 5 they added the enum
keyword and it did not break that much code either.
Although Java has no goto
, it has some constructs which correspond to some usages of goto
, namely being able to break
and continue
with named loops. Also, finally
can be thought of as a kind of twisted goto
.
The keyword exists, but it is not implemented.
The only good reason to use goto that I can think of is this:
for (int i = 0; i < MAX_I; i++) {
for (int j = 0; j < MAX_J; j++) {
// do stuff
goto outsideloops; // to break out of both loops
}
}
outsideloops:
In Java you can do this like this:
loops:
for (int i = 0; i < MAX_I; i++) {
for (int j = 0; j < MAX_J; j++) {
// do stuff
break loops;
}
}
No goto
is not used, but you can define labels and leave a loop up to the label. You can use break
or continue
followed by the label. So you can jump out more than one loop level. Have a look at the tutorial.
It's very much considered one of those things you Do Not Do, but was probably listed as a reserved word to avoid confusion for developers.