Java.Variable name length

后端 未结 4 1467
被撕碎了的回忆
被撕碎了的回忆 2021-01-28 01:11

in documentation i read, that:

A variables name can be any legal identifier — an **unlimited-length** sequence of Unicode letters and digits

so

相关标签:
4条回答
  • 2021-01-28 01:55

    The compiler has probably changed your really long name to something boring like "a".

    The variable name is there for the programmer's convenience. To the compiler and JVM it makes no difference if it's 1 character or 1 million as long as it's unique.

    0 讨论(0)
  • 2021-01-28 01:57

    The local variable names are not stored as such. They tend to be removed as part of the compiler optimization and replaced with numbers (see aload_<n> for example).

    Try to compile with javac -g or javac -g:vars, it should leave more information, which you'll be able to check with javap.

    This answer should have interesting details on this topic.

    Since your example code doesn't actually do anything anyway, it's likely to be optimized into an empty method.

    0 讨论(0)
  • 2021-01-28 02:02

    1) I wouldn't be surprised if some compiler implementations had (reasonable) limits while others could handle variable names of any length. Unfortunately, I don't know of any documented specific examples or have any specific experiences of either off-hand.

    2) Local variables names don't have to be maintained in the class file (they aren't accessible via reflection). Try making a file with an instance variable name that long and see the resulting class file.

    0 讨论(0)
  • 2021-01-28 02:05

    iiiiiii.. is a local variable, i.e. it cannot be accessed from other classes. Therefore, its name does not matter; the compiler does not need to store it. Additionally, the compiler may look at your code and determine that {int i = 10;} is not actually doing anything and can be replaced by {} since both versions produce the same program output (none).

    0 讨论(0)
提交回复
热议问题