ID compared to keyword

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

What is the difference between Keyword and identifier ? Can someone give me a simple explanation and, in addition, one or more examples for both?

+7
source share
5 answers

The terms "keyword" and "identifier" are not specific to Java.

Keyword - This is a reserved word from a list of Java keywords that provides compiler instructions. Since keywords are reserved, they cannot be used by a programmer for variable names or methods.

Examples:

 final class this synchronized 

Identifiers are the names of variables, methods, classes, packages, and interfaces. They must consist of letters, numbers, underscore _, and the dollar sign $. Identifiers can only begin with a letter, underscore, or dollar.

Examples:

 int index; String name; 

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

The keyword cannot be used as an identifier.

+6
source

Identifiers are variable names. For example, in

 int a = 3; 

a will be an identifier. On the other hand, keywords are reserved (i.e. you cannot name a variable with a keyword), predefined 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, words used to declare primitive types are also keywords, for example. int , char , long , boolean , etc. Here you can see the full list of Java keywords here.

+3
source

Keywords - reserved words like new,static,public,if,else,..

The identifier can be the name of any variable.

 int age = 26; 

"age" is the identifier, and int is the reserved word.

The following example will not compile:

 String static = "hello"; int public = 4; 

you cannot do this because "static" and "public" are keywords , which in this case are used as identifiers , which is unacceptable.

+2
source

I assume the identifier is your own (function name, var name, ...); and the keyword is "class" or "assert" or "while" - identifiers defined by the language, in other words

+1
source

The following page contains a list of Java identifiers and keywords related to the OCZ 1Z0-803 certification. Java Identifiers Keywords

0
source

All Articles