随后的字符可以是字母、数字、美元符号或下划线字符。约定(和常识)也适用于这条规则。为变量选择名称时,使用完整的单词而不是含糊不清的缩写。这样做将使您的代码更容易阅读和理解。在许多情况下,它也会让你的代码自我记录; 例如,名为韵律、速度和齿轮的字段比缩写版本(如 s、 c 和 g)更直观,同时记住,你选择的名称不能是关键字或保留字。
如果您选择的名称只包含一个单词,请用所有小写字母拼写该单词。如果它由一个以上的单词组成,则将后面每个单词的第一个字母大写。gearRatio 和 currentGear 这两个名字就是这个约定的主要例子。如果您的变量存储一个常量值,比如 static final int NUM_GEARS = 6,则约定略有变化,将每个字母大写,并用下划线字符分隔后续单词。按照惯例,下划线字符从不在其他地方使用。
This hopefully answers your second question. Regarding your first question; I've been taught both by teachers and (as far as I can remember) Java compilers that a Java class name should be an identifier that begins with a capital letter A-Z, but I can't find any reliable source on this. When trying it out with OpenJDK there are no warnings when beginning class names with lower-case letters or even a $-sign. When using a $-sign, you do have to escape it if you compile from a bash shell, however.