使用 Java RMI 时,是否存在在变量名中使用美元符号 $ 的约定?

发布于 2024-08-16 17:10:51 字数 223 浏览 5 评论 0原文

我意识到它是变量名的有效部分,但我以前从未见过变量名实际上使用符号 $ 。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ゞ花落谁相伴 2024-08-23 17:10:51

我唯一一次看到这种情况是

  • 在生成的代理类中,这些代理类经常在其方法、变量和类名
  • 内部->外部类合成访问器方法中使用 $ ,我认为这些方法也使用$ 在方法名
  • 内部类名中(如 JVM 所见),

但我从未在源代码中见过它,而且我也想不出这样做的任何理由。 $ 几乎是生成代码的明确指示符。

The only time I've ever seen this is

  • in generated proxy classes, which frequently use $ in their method, variable and class names
  • inner->outer class synthetic accessor methods, which I think also use a $ in the method name
  • inner class names (as seen by the JVM)

I'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.

雪花飘飘的天空 2024-08-23 17:10:51

但是,由于这是面向 Java 初学者的,我想知道在分布式世界中, $ 是否具有特殊含义。

本教程建议每个人都不要在人工编写的代码中的标识符中使用“$”。

事实上,JLS 也说了大致相同的内容,并且该文档绝对不是针对初学者的:

"Java 字母包括大写和小写 ASCII 拉丁字母 AZaz ,并且由于历史原因,还包括 ASCII 下划线 (_)和美元符号($$ 字符只能在机械生成的源代码中使用,或者很少用于访问预先存在的源代码。旧系统上的名称” - JLS 3.8

“$”标识符合法但不鼓励的原因是为编译器和代码生成器提供对正常标识符名称空间的“私有”扩展。当生成器需要合成标识符时,标识符中包含“$”意味着它不应与任何现有(或未来)的人类创建的标识符发生冲突。

如果“$”不可用,则生成器需要检查每个合成标识符是否不会造成标识符冲突,否则可能会产生无法编译的 Java 代码。如果生成的代码包含程序员提供的 Java 代码,那么这很困难,并且如果生成的代码需要与生成器在运行时不可用的任意类一起工作,则这可能是不可能的。

我不知道生成器实际使用“$”的程度,但我知道 Java 编译器使用它们作为嵌套和匿名内部类的名称,以及(我认为)相关的隐藏变量。所以你应该注意这个建议。

However, since this is geared toward Java beginners, I'm wondering if in the distributed world, the $ lives on with a special meaning.

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 Java letters include uppercase and lowercase ASCII Latin letters A-Z, and a-z , and, for historical reasons, the ASCII underscore (_) and dollar sign ($). The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems." - JLS 3.8.

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.

乖乖 2024-08-23 17:10:51

我只见过它故意使用过一次,那就是像 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.

美煞众生 2024-08-23 17:10:51

或者一个内部类。

不,我不会在变量名中使用它。 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.

风吹过旳痕迹 2024-08-23 17:10:51

如果您想看到现实生活中(可能)人类编写的代码广泛使用“$”进行变量命名,您可以查看 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 :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文