Java中变量或方法的最大名称长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/695951/
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
Max name length of variable or method in Java
提问by talg
Is there a max length for class/method/variable names in Java? the JLS doesn't seem to mention that. I know very long names are problematic anyway from code readability and maintainability perspective, but just out of curiosity is there a limitation (I guess class names might be limited by the file system maximal file name limitation).
Java 中的类/方法/变量名是否有最大长度?JLS似乎没有提到这一点。我知道从代码可读性和可维护性的角度来看,很长的名称无论如何都是有问题的,但出于好奇是否存在限制(我猜类名可能受到文件系统最大文件名限制的限制)。
采纳答案by talg
Sorry, actually found the answer in the JLS: http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#40625It seems that identifier names are of unlimited length.
抱歉,实际上在 JLS 中找到了答案:http: //java.sun.com/docs/books/jls/third_edition/html/lexical.html#40625似乎标识符名称是无限长度的。
Also found similar question (though it didn't appear in my initial search, or when I typed the question title which is weird): Maximum Method Name Length
还发现了类似的问题(尽管它没有出现在我的初始搜索中,或者当我输入了奇怪的问题标题时):Maximum Method Name Length
回答by Uri
If I'm not mistaken, the limit is not in the language itself but in the classfile format, which limits names to 64k, so for all practical intents and purposes identifier length is not a problem. Specifically, this is the definition of a constant string in the pool, which seems to imply the maximal length is 16 bit:
如果我没记错的话,限制不在于语言本身,而在于类文件格式,它将名称限制为 64k,因此对于所有实际意图和目的,标识符长度不是问题。具体来说,这是池中常量字符串的定义,似乎暗示最大长度为16位:
CONSTANT_Utf8_info {
u1 tag;
u2 length;
u1 bytes[length];
}
Class names may be more of an issue for file systems, I agree, I'm not sure what's currently supported.
类名对于文件系统来说可能更多是一个问题,我同意,我不确定当前支持什么。
回答by TofuBeer
If you go over the size limit imposed by the VM for method names then you get a compiler error (at least with the version of javac I am using):
如果超过 VM 对方法名称施加的大小限制,则会出现编译器错误(至少在我使用的 javac 版本中):
Main.java:1: UTF8 representation for string "aaaaaaaaaaaaaaaaaaaa..." is too long for the constant pool
Main.java:1: 字符串“aaaaaaaaaaaaaaaaaaaa...”的 UTF8 表示对于常量池来说太长了