Bytecode features not available in the Java language

后端 未结 9 1483
北恋
北恋 2020-12-04 04:28

Are there currently (Java 6) things you can do in Java bytecode that you can\'t do from within the Java language?

I know both are Turing complete, so read \"can do\"

9条回答
  •  天涯浪人
    2020-12-04 05:03

    In Java language the first statement in a constructor must be a call to the super class constructor. Bytecode does not have this limitation, instead the rule is that the super class constructor or another constructor in the same class must be called for the object before accessing the members. This should allow more freedom such as:

    • Create an instance of another object, store it in a local variable (or stack) and pass it as a parameter to super class constructor while still keeping the reference in that variable for other use.
    • Call different other constructors based on a condition. This should be possible: How to call a different constructor conditionally in Java?

    I have not tested these, so please correct me if I'm wrong.

提交回复
热议问题