關(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
標識符是程序員用來命名變量,方法,類或標簽的單詞。關(guān)鍵字和保留字不能用作標識符。標識符必須以字母,美元符號($)或下劃線(_)開頭;后續(xù)字符可以是字母,美元符號,下劃線或數(shù)字。
一些例子是:
foobar // legal Myclass // legal $a // legal 3_a // illegal: starts with a digit !theValue // illegal: bad 1st char
Java標識符區(qū)分大小寫。例如, myValue
和 MyValue
是不同的標識符。
標識符用于類名,方法名和變量名。標識符可以是大寫和小寫字母,數(shù)字或下劃線和美元符號字符的任何序列。標識符不能以數(shù)字開頭。Java標識符區(qū)分大小寫。以下代碼說明了有效標識符的一些示例:
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, } }
如果嘗試編譯此代碼,您將收到以下錯誤消息:
更多建議: