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?
Unlike C++, Java does not support the goto statement. Instead, it has label . Labels are used to change the flow of the program and jump to a specific instruction or label based on a condition.
Java doesn't have goto , because it makes the code unstructured and unclear to read. However, you can use break and continue as civilized form of goto without its problems.
The keywords const and goto are reserved, even though they are not currently used. true , false , and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.
While overall usage of gotos has been declining, there are still situations in some languages where a goto provides the shortest and most straightforward way to express a program's logic (while it is possible to express the same logic without gotos, the equivalent code will be longer and often more difficult to ...
James Gosling created the original JVM with support of goto
statements, but then he removed this feature as needless. The main reason goto
is unnecessary is that usually it can be replaced with more readable statements (like break/continue
) or by extracting a piece of code into a method.
Source: James Gosling, Q&A session
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With