Java compilers or JVM languages that support goto?

前端 未结 9 2117
孤城傲影
孤城傲影 2021-01-12 20:22

Is there a java compiler flag that allows me to use goto as a valid construct? If not, are there any third-party java compilers that supports goto?

相关标签:
9条回答
  • 2021-01-12 21:04

    The goto keyword is reserved but unused in the Java programming language. (From Section 3.9 of The Java Language Specification.)

    Therefore, at least in the Java programming language, there is no way to enable the use of goto.

    However, as already noted, the goto opcode in the Java virtual machine is functional, and used when the Java compiler produces bytecode from the source.

    Chapter 7: Compiling for the Java Virtual Machine from The Java Virtual Machine Specification may be of interest when implementing a JVM language.

    0 讨论(0)
  • 2021-01-12 21:07

    The JVM bytecode contains a goto instruction (e.g. see the BCEL documentation).

    Don't forget that Java itself supports the concept of jumping to labels, using:

    break {labelname}
    

    or

    continue {labelname}
    

    See this JDC tech tip for more info. If your language is compiled to JVM bytecode, then you may be able to make use of this.

    0 讨论(0)
  • 2021-01-12 21:09

    Pretty much anything you can do with goto you can do with a loop. goto is really redundant and generally discredited way to program. IMHO.

    If you want to goto backwards

    LABEL: do {
    // code before goto
    
    // goto LABEL
    continue LABEL;
    
    // code after goto
    break;
    } while(true);
    

    If you want to goto forwards

    LABEL: do {
    // code before goto
    
    // goto LABEL
    continue LABEL;
    
    // code after goto
    break;
    } while(false);
    // Label is effectively here
    // code after LABEL.
    
    0 讨论(0)
提交回复
热议问题