关键字-Java中的关键字向编译器传达了特殊的含义,因此,这些关键字不能用作标识符。Java提供了一组50个关键字。
| abstract | continue | for | new | switch |
| assert | default | goto | package | synchronized |
| boolean | do | if | private | this |
| break | double | implements | protected | throw |
| byte | else | import | public | throws |
| case | enum | instanceof | return | transient |
| catch | extends | int | short | try |
| char | final | interface | static | void |
| class | finally | long | strictfp | volatile |
| const | float | native | super | while |
保留字-在上述关键字列表中,关键字goto和const当前未使用。它们是保留字(供将来使用)。
true false和null -True,false和null表示Java中的某些值,它们用作文字。它们不被视为关键字。
但是,如果尝试将它们用作Java中的标识符,仍然会生成编译时错误。
public class Example {
public static void main(String args[]) {
int true = 20;
int false = 30;
float null = 23.6f;
}
}输出结果
Example.java:3: error: not a statement int true = 20; ^ Example.java:3: error: ';' expected int true = 20; ^ Example.java:4: error: not a statement int false = 30; ^ Example.java:4: error: ';' expected int false = 30; ^ Example.java:5: error: not a statement float null = 23.6f; ^ Example.java:5: error: ';' expected float null = 23.6f; ^ 6 errors