标识符的命名规则
首先需要明白标识符是用来给变量、类、方法以及包进行命名的,如Welcome、main、System、age、name等。标识符需要遵守一定规则:
- 标识符必须以字母、下划线、美元符“$”开头。
- 标识符其他部分可以是字母、下划线“_”、美元符"$"和数字的任意组合。
- Java标识符大小写敏感,且长度无限制。
- 标识符不可以是Java的关键字。
标识符的使用规范
- 表示类名的标识符:每个单词的首字母大写,如Man,GoodMan
- 表示方法和变量的标识符:第一个单词小写,从第二个单词开始首字母大写,我们称之为“驼峰原则”,如eat(),eatFood()
【注意】Java不采用通常语言使用的ASCII字符集,而是采用Unicode这样的标准的国际字符集。
【示例1】合法的标识符
int a = 3;
int _123 = 3;
int $12aa = 3;
int 变量1 = 3; // 合法,但是不建议使用中文命名标识符
【示例2】不合法的标识符
int 1a = 3; // 不能使用数字开头
int a# = 3; // 不能包含#这样的特殊字符
int int = 3; // 不能使用关键字
Java中的关键字/保留字
Java中的关键字/保留字 | |||||
abstract | assert | boolean | break | byte | case |
catch | char | class | const | continue | default |
do | double | else | extends | final | finally |
float | for | goto | if | implements | import |
instanceof | int | interface | long | native | new |
null | package | private | protected | public | return |
short | static | strictfp | super | switch | synchronized |
this | throw | throws | transient | try | void |
volatile | while |