为什么用C ++ / Java和类似的美元符号启动变量名称是不好的? [英] Why is it bad to start a variable name with a dollar sign in C++/Java and similar?

查看:188
本文介绍了为什么用C ++ / Java和类似的美元符号启动变量名称是不好的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么用C ++ / Java中的美元符号启动带有美元符号的变量名称是不好的,例如在PHP中?

Why is it bad to start a variable name with a dollar sign in C++/Java and similar such as in PHP?

编辑:是否有任何风险?

Are there any risks?

推荐答案

在Java中,在变量中使用 $ 是合法的但绝对是坏的想法。

In Java, using $ in variables is legal but definitely a bad idea.

如果这样做,您可能会意外地使用与编译器本身或某些代码使用的名称冲突的名称发电机。结果将是可能特别难以诊断的意外编译或运行时故障......

If you do this, there is a risk that you will accidentally use a name that collides with a name that is used by the compiler itself, or by some code generator. The result will be unexpected compile or runtime failures that could be particularly difficult to diagnose ...

您(错误地)使用<$ c还存在潜在风险$ c> $ 将在未来的Java版本中引发问题。 Java编译器/运行时对 $ 的使用可能在将来发生变化,导致您的滥用代码失败。

There's also a potential risk that your (mis-)use of $ will cause problems in future versions of Java. The Java compiler / runtime's use of $ may change in a future, causing your abusive code to fail.

Just不要这样做。或者至少,除非你正在编写一个生成器,否则不要这样做...... 并且你知道自己在做什么

Just don't do it. Or at least, don't do it unless you are writing a generator ... and you know what you are getting yourself into.

这篇关于为什么用C ++ / Java和类似的美元符号启动变量名称是不好的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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