本文為:Java系列教程 005:Java 關(guān)鍵字
正文共:955 字 4 圖
預(yù)計(jì)閱讀時(shí)間: 3 分鐘
1.Java 中的關(guān)鍵字完整列表
關(guān)鍵詞是其含義由編程語言定義的詞。Java 關(guān)鍵字和保留字:
abstract class extends implements null strictfp true
assert const false import package super try
boolean continue final instanceof private switch void
break default finally int protected synchronized volatile
byte do float interface public this while
case double for long return throw
catch else goto native short throws
char enum if new static transient
標(biāo)識(shí)符是程序員用來命名變量,方法,類或標(biāo)簽的單詞。
關(guān)鍵字和保留字不能用作標(biāo)識(shí)符。
標(biāo)識(shí)符必須以字母,美元符號(hào)($)或下劃線(_)開頭;
后續(xù)字符可以是字母,美元符號(hào),下劃線或數(shù)字。
一些例子是:
foobar // legal
Myclass // legal
$a // legal
3_a // illegal: starts with a digit
!theValue // illegal: bad 1st char
Java 標(biāo)識(shí)符區(qū)分大小寫。例如,myValue和MyValue是不同的標(biāo)識(shí)符。
2.使用標(biāo)識(shí)符
標(biāo)識(shí)符用于類名,方法名和變量名。
標(biāo)識(shí)符可以是大寫和小寫字母,數(shù)字或下劃線和美元符號(hào)字符的任何序列。
標(biāo)識(shí)符不能以數(shù)字開頭。Java 標(biāo)識(shí)符區(qū)分大小寫。
以下代碼說明了有效標(biāo)識(shí)符的一些示例:
public class Main {
public static void main(String[] argv) {
int ATEST, count, i1, $Atest, this_is_a_test;
}
}
以下代碼顯示無效的變量名包括:
public class Main {
public static void main(String[] argv){
int 2count, h-l, a/b,
}
}
如果嘗試編譯此代碼,您將收到以下錯(cuò)誤消息:
圖1:Java關(guān)鍵字編譯錯(cuò)誤??