Identifiers and Reserved Words

Identifiers and Reserved Words

In this tutorial, we are going to discuss about Identifiers and Reserved Words in Java. Identifiers and reserved words are fundamental concepts in any programming language. Lets first discuss about Identifiers and then we will discuss about reserved words.

It’s important to be aware of both identifiers and reserved words when writing code to avoid conflicts and ensure clarity and correctness. Using meaningful identifiers and avoiding reserved words as identifiers can make code more readable and maintainable.

Identifier and Reserved Words
Identifiers

A name in java program is called identifier which can be used for identification purpose. They are user-defined and are used to uniquely identify these elements within the program. It can be class name or method name or variable name or label name.

public class Test {
   public static void main(String[] args) {
     int a = 10;
     System.out.println("Hai This is Ashok");
   }
}

In the above example, it contains 5 identifiers. They are Test, main, String, args and a.

Rules

  • The only allowed characters in java identifiers are a to z, A to Z, 0 to 9, $ and _ (Underscore). If we are using any other character then we will get compilation error.
  • Identifiers can’t start with digit i.e., 123name is not valid.
  • Java identifiers are case sensitive of course java language itself considered as case sensitive programming language. i.e., name, Name, NAME both are different variable names.
  • There is no length limit for java identifiers but it is not recommended to take too length identifiers.
  • We can’t use reserved words as identifiers otherwise we will get compilation error. i.e., int if = 20; here we will get compilation error.
  • All predefined java class names and interface names we can use as identifiers. i.e., int String = 20; here we won’t get any compilation error. But it is not a good programming practice.
Reserved Words

Reserved words are predefined words in a programming language that have special meaning and are reserved for specific purposes. They typically cannot be used as identifiers because they are part of the language’s syntax or have predefined functionalities.

There are 53 reserved words in java. 50 are keywords and 3 are literals (true, false, null). The 50 keywords are classified in to following.

1. Reserved words for data types (8)

byte, short, int, long, float, double, boolean, char

2. Reserved words for flow control (11)

if, else, switch, case, default, while, do, for, break, continue, return

3. Reserved words for modifiers (11)

public, private, protected, final, abstract, static, synchronized, native, strictfp, transient, volatile

4. Reserved words for Exception handling (6)

try, catch, finally, throw, throws, assert (Used for debugging purpose)

5. Reserved words for class related (6)

class, interface, package, import, extends, implements

6. Reserved words for object related (4)

new, instanceof, super, this

7. Reserved words for return type (1)

void

Note

In java return type is mandatory if a method won’t return anything. Such type of methods we should declared with void return type. But in C language return type is optional and default return type is int.

8. Unused keywords (2)

goto, const

Usage of goto keyword created several problems in old languages like C and C++. Hence SUN people band this keyword in java where as we use final keyword instead of const.

Note

goto and const are unused keywords if we are trying to use then we will get compilation error.

9. enum keyword (1) (From java 1.5V)

we can use enum to define a group of named constants.

E.g

enum Month {
     JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC;
}

Note

  1. All 53 reserved words in java contain only lower case alphabets
  2. In java we have only new keyword but not delete keyword because destruction of useless objects is the responsibility of Garbage Collector.

That’s all about Identifiers and Reserved Words in Java. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java..!!

Identifiers and Reserved Words
Scroll to top