goto in Java bytecode

后端 未结 9 671
一向
一向 2020-12-16 11:58

So the other day when I was looking at the wikipedia page for Java bytecode I came across this example:

Consider the following Java code:

9条回答
  •  情话喂你
    2020-12-16 12:36

    Bytecode is some sort of assembly language for the virtual machine. It is very common to have jump instructions in machine language. goto is a unconditional jump instruction.

    The Java compiler translates almost all control flow statements inside a method body into goto instructions.

    The goto keyword was probably reserved in Java to retain the option to add it to a later version in case it would have turned out that it's existence would have been critical. There is actually nothing wrong with goto from a machine point of view. It has got a bad reputation because it allows to write code that is very hard to read for a human. The Java language allows you to use break and continue with labels as a replacement for goto.

提交回复
热议问题