简体   繁体   English

Java 对类名长度有限制吗?

[英]Does Java have a limit on the class name length?

This question came up in Spring class, which has some rather long class names.这个问题出现在 Spring 类中,它有一些相当长的类名。 Is there a limit in the language for class name lengths?类名长度的语言是否有限制?

The Java Language Specification states that identifiers are unlimited in length. Java 语言规范规定标识符的长度是无限的。

In practice though, the filesystem will limit the length of the resulting file name.但实际上,文件系统会限制生成的文件名的长度。

65535 characters I believe.我相信 65535 个字符。 From the Java virtual machine specification:来自 Java 虚拟机规范:

The length of field and method names, field and method descriptors, and other constant string values is limited to 65535 characters by the 16-bit unsigned length item of the CONSTANT_Utf8_info structure (§4.4.7).字段和方法名称、字段和方法描述符以及其他常量字符串值的长度由 CONSTANT_Utf8_info 结构(第 4.4.7 节)的 16 位无符号长度项限制为 65535 个字符。 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. UTF-8 使用两个或三个字节对某些字符进行编码。 Thus, strings incorporating multibyte characters are further constrained.因此,包含多字节字符的字符串受到进一步限制。

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

With JDK 1.5, the practical limit for class names on Windows XP with 255 -- longer names gave errors in the file system.在 JDK 1.5 中,Windows XP 上类名的实际限制为 255 —— 更长的名称会在文件系统中产生错误。 This was the full name (directory+package+class).这是全名(目录+包+类)。

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 限制。

No. Java doesn't impose any limit on the class name.不。Java 对类名没有任何限制。 But if you interfacing with other systems (eg JNI) its better to be on the safe side.但是,如果您与其他系统(例如 JNI)交互,则最好安全一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM