我对这个问题感到困惑。我们大多数人都被告知,Java中没有任何goto语句。
但我发现,它是Java中的一个关键字。哪里可以使用它呢?如果它不能被使用,那么为什么要把它作为一个关键字放在Java中?
因为它不被支持,而且你为什么想要一个什么都不做的goto
关键字或者一个名为goto
的变量?
虽然你可以使用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");
}
}