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

查看:46
本文介绍了使用单个美元符号 `$` 作为 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 平台的语言使用,并且可能需要与您的代码共存.

您目前可能不会遇到使用纯 $ 类名的技术问题(至少对于标准 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.

如果你真的,真的需要一个单字符的类名,最好安全使用 FZ 或其他没有保留的东西.

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创建一个函数库,只是想创建一个库来维护我使用的一些常用实用程序.再说一次,我是极简主义的倡导者,并且对 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天全站免登陆