Identifer versus keyword
The terms "keyword" and "identifier" are not Java specific.
A keyword is a reserved word from the Java keyword list provide the compiler with instructions. As keywords are reserved, they cannot be used by the programmer for variable or method names.
Examples:
final
class
this
synchronized
Identifiers are the names of variables, methods, classes, packages and interfaces. They must be composed of letters, numbers, the underscore _ and the dollar sign $. Identifiers may only begin with a letter, the underscore or a dollar sign.
Examples:
int index;
String name;
index
and name
are valid identifiers here. int
is a keyword.
A keyword cannot be used as an identifier.
Identifiers are names of variables. For example in
int a = 3;
a
would the identifier. Keywords, on the other hand, are reserved (i.e. you can't name a variable with a keyword), pre-defined words that have a specific meaning in the language. For example in
if (a == 3)
System.out.println("Hello World");
if
is a keyword. It has a specific function and cannot be used as a variable name. Moreover, the words used to declare primitive types are all keywords as well, e.g. int
, char
, long
, boolean
etc. You can see a full list of Java keywords here
Keywords are reserved words like new,static,public,if,else,..
An identifier can be a name of any variable.
int age = 26;
"age"
here is an identifier, while int
is a reserved word.
The following example won't compile:
String static = "hello";
int public = 4;
you can't do this because "static"
and "public"
are keywords
, that in this case are being used as identifiers
, which is not allowed.