Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 224
Venkat Avatar asked Oct 20 '22 12:10

Venkat


People also ask

Does Java have a goto statement?

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.

What is the equivalent of goto in Java?

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.

Why goto and const are not used in Java?

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.

Is goto statement still used?

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


1 Answers

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

like image 70
Vitalii Fedorenko Avatar answered Nov 12 '22 04:11

Vitalii Fedorenko