Identifier versus keyword
Asked Answered
S

5

7

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 keyword and an identifier? Can anyone give me a simple explanation and additionally one or more examples for both?

Sopping answered 22/9, 2012 at 19:27 Comment(0)
A
8

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.

Arthropod answered 22/9, 2012 at 19:34 Comment(0)
C
3

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

Cydnus answered 22/9, 2012 at 19:33 Comment(0)
O
2

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.

Oconnell answered 22/9, 2012 at 19:31 Comment(0)
W
1

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

Wheelhouse answered 22/9, 2012 at 19:30 Comment(0)
M
0

Identifiers are used to name elements of Java programs, such as classes, interfaces, variables, methods, constructors, and arguments. A valid name begins with a Unicode letter, underscore character (_), or dollar sign ($). A Unicode letter is an English letter (a-z) or (A-Z) or a letter in a non-English language. The second and remaining letters of an identifier may be a Unicode letter, underscore, dollar sign, or digit (0-9). The dollar sign character is used by compiler-internal identifiers and should be avoided.

https://www.javadeploy.com/java-certification/module2/identifiers-keywords.jsp

Managerial answered 31/12, 2015 at 17:14 Comment(1)
Link-only answers are not popular on this site. You should answer the question in the body of your answer, summarizing from a Website if necessary.Chafe

© 2022 - 2024 — McMap. All rights reserved.