当前位置: 代码迷 >> J2SE >> JAVA美元符号,该怎么处理
  详细解决方案

JAVA美元符号,该怎么处理

热度:62   发布时间:2016-04-23 20:34:10.0
JAVA美元符号
各位大神,请问美元符号开头或者下划线开头一般用在什么场合?
------解决方案--------------------
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

Naming
Every programming language has its own set of rules and conventions for the kinds of names that you're allowed to use, and the Java programming language is no different. The rules and conventions for naming your variables can be summarized as follows:
? Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_". The convention, however, is to always begin your variable names with a letter, not "$" or "_". 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. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. White space is not permitted.
? Subsequent characters may be letters, digits, dollar signs, or underscore characters. Conventions (and common sense) apply to this rule as well. When choosing a name for your variables, use full words instead of cryptic abbreviations. Doing so will make your code easier to read and understand. In many cases it will also make your code self-documenting; fields named cadence,speed, and gear, for example, are much more intuitive than abbreviated versions, such as s, c, and g. Also keep in mind that the name you choose must not be a keyword or reserved word.
? If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.



------解决方案--------------------
【翻译下二楼的】
命名
  每种编程语言都有允许你自己使用的命名规则和惯例, java也不例外。变量的命名规则和管理总结如下:
* 变量名是大小写敏感的。一个变量名可以是任何的合法标识符— 只要是以$,或者"_",或者"a-z","A-Z"字母开头的不限长度的字符或数字序列都可以,然而通常是以字母或者"_"开头,而不用"$"开头。 另外,美元符号依照惯例都是不用的。虽然你经常发现自动生成的变量名含有美元符号,但是你自己定义的变量名应该是禁止使用的。还有一个类似的习惯,虽然技术上来说,以"_"开头的变量名是合法的,但是这样的行为是不被鼓励的。当然,空格是不允许使用的。

* 第一个字符后的其它字符可以是数字,字母,美元符号或者下划线。不过约定习惯给也给我们提供了一些规则。 当选择一个变量名时,通常用单词全称而不要用奇怪的英文缩写,这样会让你的程序更易于阅读和理解。 在许多情况下可以使你的代码文档化; 例如字段名cadence, speed 和gear 比它们的缩写s, c 和g更加直观。 但是需要谨记的是不用误用了语言保留的关键字。

* 如果你的变量名只包含了一个单词,全用小写就可以了。如果包含了多于一个的单词,除了第一个单词外,其它单词首字母需要大写。例如gearRatio和currentGear。 如果你的变量包含常量,例如static final int NUM_GEARS = 6,约定就有了一些改变,需要将每个字母都大小,并且每个单词之间用下划线分开。按照约定,下划线不会用在其它的地方的。

请关注下我的公众账号 msg_filter ,互联网精选文摘,每天会为你推送关于互联网的精选咨询,谢谢!

------解决方案--------------------
如果你一个变量是数字开头的,但java是不允许数字开头的,那就前面加一个“_”,$这个就比较少见,内部类里面就有,不过不是放在开头的。
  相关解决方案