Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

goto is still a keyword in Java, although it has no use. It was saved as a keyword in the beginning in case it would be decided to add goto functionality in the future.

My question is: Why would a modern OO programming language even take into account the possibility of including goto statements in the language? As I understand goto is considered harmful and even primitive. It has no use in modern programming, never seen it in any OO code.

Why did the Java creators even consider adding gotos to the language?

share|improve this question

closed as primarily opinion-based by gnat, MichaelT, World Engineer Mar 17 at 14:34

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
in my opinion, this is because C had goto. See also: Why is “goto” a reserved word in Java? and particularly, SO question referred to in comments there –  gnat Mar 17 at 10:18
1  
This topic is sooo nineties :) –  Marcin Mar 17 at 11:04
1  
Of note, C# has and uses goto. msdn.microsoft.com/en-us/library/13940fs2.aspx with appropriate restrictions, goto is not a problem (any more than try or break or return is) –  MichaelT Mar 17 at 11:55
3  
Why are you assuming that it was "supposed to be included"? Perhaps goto is reserved so that it will never appear anywhere in a Java program. –  kdgregory Mar 17 at 12:05
    
qstn is not too bad, so ive upvoted ;-) –  zencv Mar 17 at 12:29
add comment

2 Answers 2

From this StackOverflow answer: "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."

share|improve this answer
add comment

In my experience the goto convention is still used because it produces tidier code. Less indentation and curly braces. It provides an early exit and is similar to having an early return but with the ability to tidy up.

Having a goto keyword might make the language better, so its fair enough to reserve keywords as it doesn't really cost anything to the language creators.

The people that like goto would say that goto is only dangerous when used incorrectly.

share|improve this answer
2  
I know of one example where goto produces tidier code, and that's error handling/cleanup-on-return in C. Java (and virtually all other modern languages) has different solutions for that problem (try {} finally {} and try-with-resources in Java's case) that are least as satisfactory, arguably even better. –  delnan Mar 17 at 10:32
1  
this does not even attempt to answer the question asked "Why did the Java creators even consider adding gotos to the language?" –  gnat Mar 17 at 10:48
    
@gnat Could you clarify? In the first paragraph I explain why a goto is still considered useful. Then in the second paragraph explains that it might make the language better. How do you think those two points have not addressed the question? –  Phil Hannent Mar 17 at 11:17
    
@gnat: It may not be the highest quality answer, but its certainly does attempt to answer the question asked. Unfortunately the authors attempt to head off the dogmatic point scoring that tends to come with any mention of Goto has not worked. –  mattnz Mar 18 at 2:35
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.