Java中有goto语句吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2545103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a goto statement in Java?
提问by Venkat
I'm confused about this. Most of us have been told that there isn't any goto statement in Java.
我对此感到困惑。我们大多数人都被告知 Java 中没有任何 goto 语句。
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?
但是我发现它是Java中的关键字之一。可以在哪里使用?如果它不能使用,那为什么它作为关键字包含在Java中?
采纳答案by Thomas
The Java keyword listspecifies the goto
keyword, but it is marked as "not used".
在Java的关键字列表指定的goto
关键字,但它被标记为“未使用”。
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.
它在原始 JVM 中(参见@VitaliiFedorenko 的回答),但随后被删除。它可能被保留为保留关键字,以防它被添加到更高版本的 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.
如果goto
不在列表中,并且稍后将其添加到语言中,则使用该词goto
作为标识符(变量名称、方法名称等)的现有代码将中断。但是因为goto
是关键字,这样的代码在当前甚至不会编译,并且在不破坏现有代码的情况下仍然可以让它在以后实际执行某些操作。
回答by dave
So they could be used one day if the language designers felt the need.
因此,如果语言设计者觉得有需要,它们有一天可以使用。
Also, if programmers from languages that do have these keywords (eg. C, C++) use them by mistake, then the Java compiler can give a useful error message.
此外,如果来自确实具有这些关键字的语言(例如 C、C++)的程序员错误地使用它们,那么 Java 编译器会给出有用的错误消息。
Or maybe it was just to stop programmers using goto :)
或者也许只是为了阻止程序员使用 goto :)
回答by Karl
To prohibit declarations of variables with the same name.
禁止声明同名变量。
e.g.
int i = 0, goto;
例如
int i = 0, goto;
回答by pjp
Because it's not supported and why would you want a goto
keyword that did nothing or a variable named goto
?
因为它不受支持,为什么您想要一个goto
什么都不做的关键字或一个名为 的变量goto
?
Although you can use break label;
and continue label;
statements to effectively do what goto
does. But I wouldn't recommend it.
尽管您可以使用break label;
和continue label;
语句来有效地执行该goto
操作。但我不会推荐它。
public static void main(String [] args) {
boolean t = true;
first: {
second: {
third: {
System.out.println("Before the break");
if (t) {
break second;
}
System.out.println("Not executed");
}
System.out.println("Not executed - end of second block");
}
System.out.println("End of third block");
}
}
回答by Paul Whelan
See the following link is shows all java reserved words and tells you what versions they where added.
请参阅以下链接显示所有 java 保留字并告诉您它们添加的版本。
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html
goto is reserved, even though it is not currently used, never say never however :)
goto 是保留的,即使它当前没有使用,但是永远不要说永远:)
回答by starblue
Note that you can replace most of the benign uses of goto by
请注意,您可以通过以下方式替换 goto 的大多数良性用途
return
break
break label
throw inside try-catch-finally
返回
休息
打破标签
扔进去 try-catch-finally
回答by Bozho
No, thankfully, there isn't goto
in Java.
不,谢天谢地,goto
Java 中没有。
The goto
keyword is only reserved, but not used (the same goes for const
).
该goto
关键字仅保留,但不使用(也是如此const
)。
回答by Isaac E
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html
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. "
“关键字 const 和 goto 是保留的,即使它们当前未使用。”
回答by Michael Borgwardt
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.
不,goto
尽管是保留字,但在 Java 中不使用。对于 也是如此const
。这两个都在C++中使用,这可能是它们被保留的原因;这样做的目的可能是为了避免使迁移到 Java 的 C++ 程序员感到困惑,并且可能还保留在 Java 的后续修订版中使用它们的选项。
回答by Heinzi
They are reserved for future use (see: Java Language Keywords)
它们保留供将来使用(请参阅:Java 语言关键字)
The keywords const and goto are reserved, even though they are not currently used.
关键字 const 和 goto 是保留的,即使它们当前未使用。
The reason whythere is no goto statement in Java can be found in "The Java Language Environment":
究其原因,为什么没有在Java中没有goto语句可以在“发现Java语言环境”:
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.
Java 没有 goto 语句。研究表明,goto 被(误)使用得更频繁,而不仅仅是“因为它就在那里”。消除 goto 导致了语言的简化——例如,没有关于 goto 进入 for 语句中间的效果的规则。对大约 100,000 行 C 代码的研究确定,大约 90% 的 goto 语句纯粹用于获得打破嵌套循环的效果。如上所述,多级 break 和 continue 消除了对 goto 语句的大部分需求。