Java 对类名长度有限制吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1039029/
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 22:35:44  来源:igfitidea点击:

Does Java have a limit on the class name length?

javaname-length

提问by Eugene Katz

This question came up in Spring class, which has some rather long class names. Is there a limit in the language for class name lengths?

这个问题出现在 Spring 类中,它有一些相当长的类名。类名长度的语言是否有限制?

采纳答案by Robert Munteanu

The Java Language Specificationstates that identifiers are unlimited in length.

Java语言规范指出标识符的长度不受限制。

In practice though, the filesystem will limit the length of the resulting file name.

但实际上,文件系统会限制生成的文件名的长度。

回答by lud0h

No. Java doesn't impose any limit on the class name. But if you interfacing with other systems (e.g. JNI) its better to be on the safe side.

不。Java 对类名没有任何限制。但是,如果您与其他系统(例如 JNI)交互,则最好安全一点。

回答by Jon

65535 characters I believe. From the Java virtual machine specification:

我相信 65535 个字符。来自 Java 虚拟机规范:

The length of field and method names, field and method descriptors, and other constant string values is limited to 65535characters by the 16-bit unsigned length item of the CONSTANT_Utf8_info structure (§4.4.7). Note that the limit is on the number of bytes in the encoding and not on the number of encoded characters. UTF-8 encodes some characters using two or three bytes. Thus, strings incorporating multibyte characters are further constrained.

字段和方法名称、字段和方法描述符以及其他常量字符串值的 长度由 CONSTANT_Utf8_info 结构(第 4.4.7 节)的 16 位无符号长度项限制为 65535 个字符。请注意,限制是编码中的字节数而不是编码字符数。UTF-8 使用两个或三个字节对某些字符进行编码。因此,包含多字节字符的字符串受到进一步限制。

here:

这里:

https://docs.oracle.com/javase/specs/jvms/se6/html/ClassFile.doc.html#88659

https://docs.oracle.com/javase/specs/jvms/se6/html/ClassFile.doc.html#88659

回答by Stanleymit

With JDK 1.5, the practical limit for class names on Windows XP with 255 -- longer names gave errors in the file system. This was the full name (directory+package+class).

在 JDK 1.5 中,Windows XP 上类名的实际限制为 255 —— 更长的名称会在文件系统中产生错误。这是全名(目录+包+类)。

I have not tried JDK 1.6 on Vista or windows 7, hopefully Sun fixed it to be the NTFS limit of 8000 or so.

我还没有在 Vista 或 Windows 7 上尝试过 JDK 1.6,希望 Sun 将其修复为 8000 左右的 NTFS 限制。