Java Made Clear: Identifier vs Keyword

Identifier vs Keyword are both very different in any language. Their purpose is very specific.
Identifier vs Keyword: What is Identifier?

An identifier identifies something in a programming language. It may be, in Java,

a) a variable
b) a method
c) a class
d) a package
e) an interface

So finally, an identifier, in Java, can be a variable or method or a class or interface or a package. For example,

int marks = 50; Here, marks is a variable identifier that identifies marks of a student
public void calculate() { } Here, the method is identified as calculate that calculates something
public class Test { } Here, the class is identified as Test which tests something in its code

To repeat again:

Identifier is a name. Using identifier, we can name a variable like marks, or method like calculate() or class like Test. Or to say, the names like marks, calculate and Test are known as identifiers.

Identifiers cannot be named just like that fancily. To name an identifier, rules exists in Java (ofcourse, in every language). Rules should be followed in coding, else the compiler rejects the compilation. The rules are clearly discussed in Java Made Clear: Rules of Identifiers

Coming to Java, unlike in C/C++, to name the identifier some conventions (not rules) exist. Conventions increase readability. Conventions are clearly discussed in Java Naming Conventions – Readability

Identifier vs Keyword: What is Keyword?

In a programming language, a keyword has a special meaning to the compiler. A keyword, as the name implies, keys (conveys) some information to the compiler. Finally, a keyword passes some extra information to the compiler or JVM.

For example,

int marks = 50;

Here, int is a keyword conveying special meaning that marks variable should be assigned an integer value. If marks is given a floating-point value (like int marks=45.5), the compiler raises error.

public void calculate() { }

Here, public is a keyword (known as access specifier) which conveys meaning to Java that to allow the calculate() method to be used by any other class from any package.

final int marks = 50;

Here, final is a keyword that informs the compiler to see that marks value cannot be changed in the program. Now marks variable properties are given by two keywords – final and int.

Not to have confusion, observe,

final int marks = 50;

Here, final and int are keywords, marks is an identifier (identifying a variable) and the value of the variable is 50.

Java comes 53 words out of which 48 are keywords, 2 are reserved words and 3 literals. They are shown in Java Keywords

Leave a Comment

Your email address will not be published.