Java 中 goto 语句的替代方案
Java 中 goto 关键字的替代函数是什么?
因为Java没有goto。
What is an alternative function for the goto keyword in Java?
Since Java does not have a goto.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您可以使用带标签的 BREAK 语句:
但是,在正确设计的代码,您不需要 GOTO 功能。
You could use a labeled BREAK statement:
However, in properly designed code, you shouldn't need GOTO functionality.
Java 中没有任何与
goto
概念直接等效的东西。有一些结构可以让您执行一些使用经典的 goto 可以执行的操作。break
和continue
语句允许您跳出循环或 switch 语句中的块。break
允许您跳出任意复合语句到给定方法(或初始化块)内的任何级别。continue
从内循环继续外循环的下一次迭代。返回
。这些 Java 构造都不允许您向后分支或分支到代码中与当前语句处于同一嵌套级别的点。它们都跳出一个或多个嵌套(范围)级别,并且它们(除了
继续
)都向下跳转。此限制有助于避免旧版 BASIC、FORTRAN 和 COBOL 代码中固有的 goto“意大利面条代码”综合症2。1- 异常最昂贵的部分是异常对象及其堆栈跟踪的实际创建。如果您确实需要使用异常处理来进行“正常”流程控制,您可以预分配/重用异常对象,或者创建一个重写 fillInStackTrace() 方法的自定义异常类。缺点是异常的
printStackTrace()
方法不会为您提供有用的信息...如果您需要调用它们。2 - 意大利面条代码综合症催生了结构化编程方法,限制对可用语言结构的使用。这可以应用于 BASIC、Fortran 和 COBOL,但它需要照顾和纪律。完全摆脱
goto
是一个更实用的解决方案。如果你把它保留在一种语言中,总会有一些小丑会滥用它。There isn't any direct equivalent to the
goto
concept in Java. There are a few constructs that allow you to do some of the things you can do with a classicgoto
.break
andcontinue
statements allow you to jump out of a block in a loop or switch statement.break <label>
allow you to jump out of an arbitrary compound statement to any level within a given method (or initializer block).continue <label>
to continue with the next iteration of an outer loop from an inner loop.return
.None of these Java constructs allow you to branch backwards or to a point in the code at the same level of nesting as the current statement. They all jump out one or more nesting (scope) levels and they all (apart from
continue
) jump downwards. This restriction helps to avoid the goto "spaghetti code" syndrome inherent in old BASIC, FORTRAN and COBOL code2.1- The most expensive part of exceptions is the actual creation of the exception object and its stacktrace. If you really, really need to use exception handling for "normal" flow control, you can either preallocate / reuse the exception object, or create a custom exception class that overrides the
fillInStackTrace()
method. The downside is that the exception'sprintStackTrace()
methods won't give you useful information ... should you ever need to call them.2 - The spaghetti code syndrome spawned the structured programming approach, where you limited in your use of the available language constructs. This could be applied to BASIC, Fortran and COBOL, but it required care and discipline. Getting rid of
goto
entirely was a pragmatically better solution. If you keep it in a language, there is always some clown who will abuse it.只是为了好玩,这里是一个 GOTO 实现在爪哇。
我需要添加“不要使用它!”吗?
Just for fun, here is a GOTO implementation in Java.
Do I need to add "don't use it!"?
虽然一些评论者和反对者认为这不是 goto,但从下面的 Java 语句生成的字节码确实表明这些语句确实表达了 goto 语义。
具体来说,第二个示例中的 do {...} while(true); 循环由 Java 编译器优化,以便不评估循环条件。
在字节码中向前跳转
:
在字节码中向后跳转
:
While some commenters and downvoters argue that this isn't goto, the generated bytecode from the below Java statements really suggests that these statements really do express goto semantics.
Specifically, the
do {...} while(true);
loop in the second example is optimised by Java compilers in order not to evaluate the loop condition.Jumping forward
In bytecode:
Jumping backward
In bytecode:
如果你真的想要像 goto 语句这样的东西,你总是可以尝试打破命名块。
您必须在块的范围内才能中断标签:
我不会告诉您为什么应该避免 goto - 我假设您已经知道答案。
If you really want something like goto statements, you could always try breaking to named blocks.
You have to be within the scope of the block to break to the label:
I won't lecture you on why you should avoid goto's - I'm assuming you already know the answer to that.
斯蒂芬·C 写道:
还有一件事……
马特·沃尔夫写道:
http://docs.oracle.com/javase/tutorial/essential/exceptions /finally.html
与finally相关的一个愚蠢的面试问题是:如果你从try{}块返回,但你的finally{}中也有一个返回,那么返回哪个值?
StephenC writes:
One more...
Matt Wolfe writes:
http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
The silly interview question associated with finally is: If you return from a try{} block, but have a return in your finally{} too, which value is returned?
最简单的是:
The easiest is:
尝试下面的代码。这对我有用。
Try the code below. It works for me.
在现代 Java 中,我只使用 switch 和 String。我很惊讶我找不到这个答案:
In modern Java I would just use switch and String. I am surprised that I couldn't find this answer:
使用带标签的break 作为 goto 的替代方案。
Use a labeled break as an alternative to goto.
Java 没有
goto
,因为它使代码非结构化且难以阅读。但是,您可以将break
和continue
作为 goto 的文明形式使用,而不会出现任何问题。使用break向前跳转 -
输出:
使用 continue 向后跳转
这将导致无限循环,因为每次执行
continue before
行时,< strong>代码流程将从之前
重新开始。Java doesn't have
goto
, because it makes the code unstructured and unclear to read. However, you can usebreak
andcontinue
as civilized form of goto without its problems.Jumping forward using break -
Output:
Jumping backward using continue
This will result in an infinite loop as every time the line
continue before
is executed, the code flow will start again frombefore
.