goto in Java bytecode

后端 未结 9 655
一向
一向 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:18

    You should distinguish between language keyword goto and byte code or assembly instruction goto.

    It is bad practice to use goto jumps in high level code, like in C. Therefore it is not allowed in Java.

    The original Edsger W. Dijkstra paper on goto.

    In compiled code usage of unconditional jump instruction goto is completely OK. It is put in there by compiler and it does not forget about implications of jumping around the code including initialization of data, deallocating memory etc.

    0 讨论(0)
  • 2020-12-16 12:19

    General background info:

    The hardware part of any microprocessor only knows it needs to successively execute every instruction starting from a memory address -- it doesn't even know at which memory address to stop executing instructions.

    Assembly language is a very thin converter from "commands" to "binary microinstructions". The list of "commands" does not include control flow statements at all all you have is jump instructions (simple jumps or conditional jumps), that's it (okay, there is an instruction for unconditional endless loops and for conditional loops).

    Because of this the control flow statements which are available in higher languages such as C are implemented using these jump instructions because there is no other way to implement them. As it so happens goto in C is compiled into binary instructions as just a simple unconditional jump instruction.

    Java & JVM rationale:

    Many different hardware architectures have different standards/formats for "binary microinstructions" and different sets of instructions. The JVM has its own standard and its own set of instructions.

    This allows the Java Compiler to always output the same instructions no matter on what hardware architecture the executable will be run; it's the JVM's job to translate an instruction from its own standard into the current machine's standard.

    So in essence the JVM bytecode is an "assembly language" for "the java virtual machine". Which means it doesn't have control flow instructions. It has unconditional jump instructions (which happen to be named goto).

    break and continue at the lowest level happen to be implemented as a jump (or goto). The point of the matter is that if you use a higher level language you would want to avoid using goto even if it's available (like in C), and would use the more readable control structures.

    There are some special cases (in C for ex.) when even programmers who respect all of the "best coding practices" would use a goto for example coroutine implementations.

    Some other examples of sacrificing the coding standards for better or more reliable performance is when Kernel developers have architecture specific assembly code (C allows you to write assembly instructions).

    0 讨论(0)
  • 2020-12-16 12:19

    goto is fine at machine level, java compiler are not writing code, it only converts code from java source to bytecode.

    For people writing code this is diffrent story, goto instruction are hard to read and analyze and code is the mess after many goto jumps. This is why people should use OO concepts instead of jump instructions.

    0 讨论(0)
  • 2020-12-16 12:23

    bytecode isn't Java, programs in other languages, like Groovy, can be compiled into bytecode, you can write bytecode directly using some tools like BCEL. As for goto you cannot go without it at low level.

    0 讨论(0)
  • 2020-12-16 12:27

    You're looking at the JVM's equivalent of machine code. Whether goto is permitted in Java is irrelevant to whether it's permitted in the bytecode, just as pointers aren't permitted in JVM bytecode, but the JVM will certainly compile or interpret the bytecode into machine code that does use pointers.

    0 讨论(0)
  • 2020-12-16 12:28

    Goto statements in programming are one way statements where as function calls are two way switch that is it will return back to the called section of the code.

    To use those only bytecodes use goto in them. In case if user is allowed to use goto means we may use it in a inefficient way (say unconditioned goto statement) which will never let the program to terminate.

    Jvm is such intelligent that never let the program run infinitely.

    0 讨论(0)
提交回复
热议问题