Java 类中允许的最大代码行数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/107855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:23:19  来源:igfitidea点击:

Maximum lines of code permitted in a Java class?

java

提问by kiritsinh parmar

How many lines of code can a .javafile contain? Does it depend on the JVM being used?

一个.java文件可以包含多少行代码?它是否取决于正在使用的 JVM?

回答by Jonas Klemming

I believe there is a 64kb limit on bytecode size per method.

我相信每个方法的字节码大小有 64kb 的限制。

回答by coobird

To extend upon Jonas's response, the Java Virtual Machine Specification, Section 4.8 Constraints on Java Virtual Machine Codesays that:

为了扩展Jonas 的回应,Java Virtual Machine Specification, Section 4.8 Constraints on Java Virtual Machine Code说:

The Java virtual machine code for a method, instance initialization method (§3.9), or class or interface initialization method (§3.9) is stored in the code array of the Code attribute of a method_info structure of a class file. This section describes the constraints associated with the contents of the Code_attribute structure.

方法、实例初始化方法(第 3.9 节)或类或接口初始化方法(第 3.9 节)的 Java 虚拟机代码存储在类文件的 method_info 结构的 Code 属性的代码数组中。本节描述与 Code_attribute 结构的内容相关的约束。

Continuing to Section 4.8.1, Static Constraints

继续第 4.8.1 节,静态约束

The static constraints on a class file are those defining the well-formedness of the file. With the exception of the static constraints on the Java virtual machine code of the class file, these constraints have been given in the previous section. The static constraints on the Java virtual machine code in a class file specify how Java virtual machine instructions must be laid out in the code array and what the operands of individual instructions must be.

The static constraints on the instructions in the code array are as follows:

...

  • The value of the code_length item must be less than 65536.

...

类文件的静态约束是那些定义文件格式良好的约束。除了class文件的Java虚拟机代码上的静态约束之外,这些约束在上一节中已经给出。类文件中 Java 虚拟机代码的静态约束指定 Java 虚拟机指令必须如何在代码数组中布局以及单个指令的操作数必须是什么。

代码数组中指令的静态约束如下:

...

  • code_length 项的值必须小于 65536。

...

So a method does have a limit of 65535 bytes of bytecode per method. (see note below)

因此,一个方法的每个方法的字节码限制为 65535 字节。(见下面的注释)

For more limitations to the JVM, see Section 4.10 Limitations of the Java Virtual Machine.

有关 JVM 的更多限制,请参阅第 4.10 节 Java 虚拟机的限制

Note: Although there is apparently a problem with the design of the JVM, where if the instruction at byte 65535 is an instruction that is 1 byte long, it is not protected by exception handler - this is listed in footnote 4 of Section 4.10.

注意:虽然JVM的设计显然有问题,如果65535字节的指令是1字节长的指令,它不受异常处理程序的保护——这在4.10节的脚注4中列出。

回答by Henning

I remember actually running into this limit once in a complex JSP page in Tomcat 4 (way in the past when people were still using JSPs). The java file generated from the JSP had a method that was too big to compile, I think I had to split up the file or do some other stunt, which of course was a good idea in terms of readability anyway.

我记得曾经在 Tomcat 4 的复杂 JSP 页面中遇到过这个限制(过去人们仍在使用 JSP 时的方式)。JSP 生成的java 文件有一个方法太大而无法编译,我想我必须拆分文件或做一些其他的特技,这当然在可读性方面是个好主意。

Sun's bug tracker tells me some people still have the same problem.

Sun 的错误跟踪器告诉我,有些人仍然有同样的问题

回答by Henning

There is no limit on "lines of code", but there is a limit on total size. Each method has a 64kb limit.

“代码行数”没有限制,但总大小有限制。每种方法都有 64kb 的限制。

I've only ever run into this with code generation tools.

我只用代码生成工具遇到过这个问题。

If you are coming close to the limit, be careful. A lot of profiling and monitoring tools use byte code insertion. They can push you over the top if you're too close. What's worse is that they often alter your class files after compilation. Everything compiles and runs in your development environment, but it crashes when you turn on your monitoring tools in Test or QA.

如果您接近极限,请小心。许多分析和监控工具使用字节码插入。如果您离得太近,他们可能会将您推到顶部。更糟糕的是,他们经常在编译后更改您的类文件。一切都在您的开发环境中编译和运行,但是当您在测试或 QA 中打开监控工具时它会崩溃。

回答by Nadhu

As mentioned the above, there is no limit on "lines of code” per class in java, we can probably use 200 lines as a good guideline and not exceed 500 lines per class.

如上所述,java中每个类的“代码行数”没有限制,我们大概可以使用200行作为一个很好的指导方针,每个类不超过500行。