Java中变量名的长度限制 [英] Length limit for a variable name in Java

查看:358
本文介绍了Java中变量名的长度限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Java中变量或方法的最大名称长度

我正在阅读Java文档,它说变量名可以是任何合法标识符-无限长度的Unicode字母和数字序列……在c ++中,变量名长度大约为255个字符,具体取决于编译器,那么在java中如何处理呢?编译器会在x个字符后截断变量名吗?如果是,那么x是什么?

I was reading the java docs and it says "A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits … " in c++ the variable name length is around 255 characters depending on the compiler, so how is this handled in java does the compiler truncate the variable name after x number of characters, and if this is true what would be x ?

推荐答案

根据类文件格式规范(在第4.11节中):

According to the class file format spec (under section 4.11):


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

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). 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.

由于 LocalVariableTable 指向变量名称的CONSTANT_Utf8_info值。

This applies to local variables as well because of the LocalVariableTable pointing to CONSTANT_Utf8_info values for the variable names.

这篇关于Java中变量名的长度限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆