使用单个美元符号`$`作为java类名称的任何风险? [英] Any risk using a single dollar sign `$` as a java class name?

查看:139
本文介绍了使用单个美元符号`$`作为java类名称的任何风险?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初我使用下划线 _ 作为类名。新的Java8编译器抱怨它在Java SE 8 之后可能不支持。我将其更改为 $ ,并且不再有任何警告。但是我记得Java使用 $ 来表示字节代码中的内部/嵌入类。我想知道是否有任何风险使用美元符号 $ 作为班级名称

Originally I was using the underscore _ as a class name. The new Java8 compiler complains that it "might not be supported after Java SE 8". I changed that to $, and there is no warning any more. However I remember that $ is used by Java to indicate an inner/embedded class in the byte code. I am wondering if there is any risk to use a dollar sign $ as a class name

此背景信息题。我想要做的是克服Java不支持纯函数的事实,而_或$是将命名空间封装一些非常通用的概念(类/静态方法)。我也没有一个好名字,也不希望lib用户输入太多东西来引用该命名空间。以下代码显示了我正在做的事情: https://github.com/greenlaw110/java-tool/blob/master/src/main/java/org/osgl/_.java

Some background to this question. What I want to do is to overcome the fact that Java doesn't support pure function, and the _ or $ is to put an namespace to encapsulate some very generic concept (classes/static methods). and neither do I have a good name for this, nor do I want the lib user type too many things to reference that namespace. Here is the code showing what I am doing under the way: https://github.com/greenlaw110/java-tool/blob/master/src/main/java/org/osgl/_.java

推荐答案

在Java中的任何标识符中使用 $ 都是不好的风格,并且可能存在风险。它有风险的原因是 $ 字符保留用于使用Java工具链和第三方语言工具。

It is bad style, and potentially risky to use $ in any identifier in Java. The reason it is risky is that the $ character is reserved for the use of the Java toolchain and third-party language tools.


  • Java编译器在内部和嵌套类的内部类名中使用它。

  • 它由Java编译器以合成属性的名称使用。

  • 它可以被第三方代码生成器(例如注释处理器)用于各种目的。

  • 它可以被其他针对JVM平台的语言使用,并且可能需要与您的代码共存。

  • It is used by Java compilers in "internal" class names for inner and nested classes.
  • It is used by Java compilers in the names of synthetic attributes.
  • It could be used by third-party code generators (e.g. annotation processors) for various purposes.
  • It could be used by other languages that target the JVM platform, and that might need to co-exist with your code.

目前,您可能不会遇到普通 $ 类名的技术问题(至少对于标准Java工具链而言)。但总有可能在未来发生变化:

You probably won't have technical issues with a plain $ classname at the moment (at least with respect to the standard Java toolchain). But there's always the possibility that this will change in the future:


  • 他们(有效地)保留正确的改变这个 1

  • _ 示例中有一个先例可以做到这一点。

  • They have (effectively) reserved the right to change this1.
  • There is a precedent for doing this in the _ example.

如果你真的需要一个单字符的类名,最好安全地使用它并使用 F Z 或其他未保留的内容。

If you really, really need a one-character classname, it would be better to play it safe and use F or Z or something else that isn't reserved.

但说实话,我认为你最好尝试实现(或者只是使用)一种真正的函数式语言,而不是试图将功能性编程系统转化为Java。或者,也许只是在官方发布之前切换到Java 8。 '因为我会拒绝拒绝来读取/维护一个看起来像jquery的Java代码库。

But to be honest, I think you'd be better off trying to implement (or just use) a real functional language than trying to shoe-horn a functional programming "system" into Java. Or maybe, just switch to Java 8 ahead of its official release. 'Cos I for one would refuse to read / maintain a Java codebase that looked like jquery.

我不是要为Java创建一个函数库,只是想创建一个lib来维护我使用的一些常用工具。再一次,我是极简主义的倡导者,并对像apache commons这样的东西感到厌倦。添加功能的东西,以帮助我更容易操作集合。

I don't mean to create a functional lib for Java, just want to create a lib to maintain some common utilities I used. Again, I am a advocate of minimalism and feel suck with things like apache commons. The functional stuff is added to help me easier to manipulate collection(s).

如果是你的代码,你可以做你做的事情喜欢。做出自己的决定。根据你的意见行事。成为风险承担者...... :-)。 (我们对 $ 的建议,等等......是没有意义的。)

If it is your code, you can do what you like. Make your own decisions. Act on your opinions. Be a "risk taker" ... :-). (Our advice on $, etcetera ... is moot.)

但如果你正在写这个代码客户或雇主,或打算创建(可行的)开源产品,那么您需要考虑其他人的意见。例如,如果您在其他地方找到更好的工作,您的老板需要对您的代码的可维护性有一个明智的意见。一般来说,下一个人是否能够弄清楚,保留你的代码,新鲜等等......还是会被托付给垃圾箱?

But if you are writing this code for a client or employer, or with the intention of creating a (viable) open source product, then you need to take account of other people's opinion. For example, your boss needs to have an informed opinion on how maintainable your code will be if you find a better paying job somewhere else. In general, will the next guy be able to figure it out, keep your code, fresh, etc ... or will it be consigned to the dustbin?

1 - JLS§3.8声明 $ 字符只能用于机械生成的源代码。这就是说使用它会带来危险。假设构建自己的源代码生成器的人可以更改它们,如果标准工具链使用裸 $ ...但它更难改变大量手写代码,这将成为升级的障碍。

1 - JLS §3.8 states "The $ character should be used only in mechanically generated source code". That is saying "use it at your peril". The assumption is that folks who build their own source code generators can change them if the standard toolchain uses a bare $ ... but it is harder to change lots of hand written code, and that would be an impediment to upgrading.

这篇关于使用单个美元符号`$`作为java类名称的任何风险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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