Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

Why does Java limit the size of a method to 65535 byte?

I just compiled the following code

public class A {
    public static void main(String... args) {
        int i = 3;
        ++i; 
        ++i;
        ++i;
        ++i;
        ++i;
        ++i;
        ++i;
        ++i;
        // repeat writing the expression ++i for 20,000 times

        System.out.println(i);
    }
}

And got the following error message

The code of method main(String...) is exceeding the 65535 bytes limit

Why does Java implement this limit? I don't see the rational since Java does include a goto_w instruction.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See the Java Virtual Machine Specification section 4.10:

4.10 Limitations of the Java Virtual Machine

  • The amount of code per non-native, non-abstract method is limited to 65536 bytes by the sizes of the indices in the exception_table of the Code attribute (§4.7.3), in the LineNumberTable attribute (§4.7.8), and in the LocalVariableTable attribute (§4.7.9).

There's few good reasons to have a method that long in an object-oriented programming language.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...