reporter reporter - 6 months ago 29
Java Question

Identifer versus keyword

I read in the book for OCJP for Java6 the part with assertions. I reached the part where it gives me an overview of how the compiler reacts if the word 'assert' is used as keyword or as an identifier.

What is the difference between a

and an
? Can anyone give me a simple explanation and additionally one or more examples for both?


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.



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.


int index;
String name;

index and name are valid identifiers here. int is a keyword.

A keyword cannot be used as an identifier.