使用 Java RMI 时,是否存在在变量名中使用美元符号 $ 的约定?
我意识到它是变量名的有效部分,但我以前从未见过变量名实际上使用符号 $ 。
Java 教程是这样说的:
此外,按照惯例,美元符号字符根本不会被使用。您可能会发现某些情况下自动生成的名称将包含美元符号,但您的变量名称应始终避免使用它。
然而,由于这是面向 Java 初学者的,我想知道在分布式世界中, $ 是否具有特殊的含义。
I realize that it is a valid part of a variable name, but I've never seen variable names actually use the symbol $ before.
The Java tutorial says this:
Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it.
However, since this is geared toward Java beginners, I'm wondering if in the distributed world, the $ lives on with a special meaning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我唯一一次看到这种情况是
$
,我认为这些方法也使用$
在方法名但我从未在源代码中见过它,而且我也想不出这样做的任何理由。
$
几乎是生成代码的明确指示符。The only time I've ever seen this is
$
in their method, variable and class names$
in the method nameI've never seen it used in source code, though, and I can't think of any reason for doing so. The
$
is almost a definitive indicator of generated code.本教程建议每个人都不要在人工编写的代码中的标识符中使用“$”。
事实上,JLS 也说了大致相同的内容,并且该文档绝对不是针对初学者的:
“$”标识符合法但不鼓励的原因是为编译器和代码生成器提供对正常标识符名称空间的“私有”扩展。当生成器需要合成标识符时,标识符中包含“$”意味着它不应与任何现有(或未来)的人类创建的标识符发生冲突。
如果“$”不可用,则生成器需要检查每个合成标识符是否不会造成标识符冲突,否则可能会产生无法编译的 Java 代码。如果生成的代码包含程序员提供的 Java 代码,那么这很困难,并且如果生成的代码需要与生成器在运行时不可用的任意类一起工作,则这可能是不可能的。
我不知道生成器实际使用“$”的程度,但我知道 Java 编译器使用它们作为嵌套和匿名内部类的名称,以及(我认为)相关的隐藏变量。所以你应该注意这个建议。
The Tutorial's advice to not use '$' in identifiers in human-written code is addressed to everyone.
Indeed, the JLS says roughly the same thing, and that document is most decidedly NOT aimed at beginners:
The reason that '$' identifiers are legal but discouraged is to provide compilers and code generators a "private" extension to the normal identifier namespace. When a generator needs to synthesize an identifier, including a '$' in the identifier means that it should not collide with any existing (or future) human-created identifiers.
If '$' was not available, the generator would need to check that each synthesized identifier does not create an identifier collision, or risk generating Java code that does not compile. This is difficult if the generated code includes Java code supplied by the programmer, and potentially impossible if the generated code needs to work with arbitrary classes that are not available to the generator at the time it is run.
I don't know the extent to which '$' is actually used by generators, but I know for a fact that the Java compiler uses them for names of nested and anonymous inner classes, and (I think) related hidden variables. So you should pay attention to this advice.
我只见过它故意使用过一次,那就是像 BigInteger $33 = BigInteger.valueOf(33L);
看起来很奇怪。
I've only seen it used once intentionally, and that was for something like
BigInteger $33 = BigInteger.valueOf(33L);
It looks weird.
或者一个内部类。
不,我不会在变量名中使用它。 RMI 也没有什么特别之处来保证这样的选择。
Or an inner class.
No, I wouldn't use it in a variable name. There's nothing special about RMI to warrant such a choice, either.
如果您想看到现实生活中(可能)人类编写的代码广泛使用“$”进行变量命名,您可以查看 PacketWorld(多代理系统的模拟工具)。
我刚刚开始使用它,并在谷歌搜索“为什么在 java 中使用美元?”后被发送到这里。
还是没有意义:(
If you want to see real-life (probably) human-written code making extensive use of '$' for variable naming, you can have a look at PacketWorld (a simulation tool for multi-agent systems).
I just started working with it and was sent here after a google search on 'Why use dollar in java?'.
Still doesn't make sense :(