Identifiers in Java | Rules for Identifiers

Identifiers in Java are names that identify elements such as classes, variables, and methods in a program.

In other words, an identifier is one that is for naming variables, user-defined methods, classes, objects, parameters, labels, packages, and interfaces in a program.

In Java, an identifier is a sequence of letters, digits, and underscore characters.

For example, Student, main, length, breadth, num1, num2, number, area, and so on are names of things that appear in the program.

In Java programming terminology, such names are called identifiers. They are case sensitive, which means that area, Area, and AREA are not the same. All are different identifiers.

Rules for naming Identifiers in Java

Java identifiers obey the following rules that are as follows:

1. Java identifier is a sequence of characters that consists of letters, digits, underscores (_), and dollar signs ($).

2. It must start with a letter, an underscore (_), or a dollar sign ($). It cannot begin with a digit.

3. It cannot be a reserved word.

4. It cannot be true, false, or null.

5. It can be of any length.

Identifiers in Java should be meaningful and can be easily read. There are some naming conventions that are followed by Java programmers. They are as:

1. The names of all public instance variables and methods begin with a leading lowercase (small) letter. For example, average, sum, etc.

2. If more than one word is used in a name, the second and subsequent words must start with a leading uppercase letter. For example, dayTemperature, totalMarks, schoolId, etc.

3. All private and local variables start with only lowercase letters combined with underscores. For example, length, student_id, etc.

4. All classes and interfaces must begin with a leading uppercase letter and each subsequent word with a leading uppercase letter. For example, Student, HelloJava, Vehicle, etc.

5. Variables that are initialized with constant values, must start with a leading uppercase letter and can place underscores between words. For example, TOTAL, PRINCIPAL_AMOUNT, etc.

Note that all the above points are conventions, not rules.

Q. Which of the following identifiers are valid? Which are Java keywords?

id, Test, Student, main, a++, ––a, 4#R, $4, #44, apps, class, public, abstract, int, x, y, radius, boolean

Ans: id, Test, Student, main, $4, apps, x, y, radius are all valid identifiers. Whereas, class, public, boolean, abstract, and int are all reserved keywords.

Hope that this tutorial has covered all the important points related to identifiers in Java with suitable examples. I hope that you will have understood the rules of identifiers.
Thanks for reading!!!
Next ⇒ Comments in Java

⇐ Prev Next ⇒