Identifiers and Reserved Words in Java

Identifiers and Reserved Words in Java with Examples

In this article, I am going to discuss the Identifiers and Reserved Words in Java with Examples. Please read our previous article, where we discussed Variables in Java with examples. At the end of this article, you will understand what are Identifiers and its need as well as you will also understand Java Reserved Words which are also called as java Keywords.

Identifiers in Java:

All Java components require names. Name used for classes, methods, interfaces, and variables are called identifiers. It allows a programmer to refer to the item from other places in the program. There are some reserved words in Java, which we can’t use as identifiers. In addition, you have to follow some rules before declaring an identifier. So, let’s discuss everything in detail.

For example int age; Here, age is a variable (an identifier). You cannot use the keyword as a variable name because keywords have predefined meanings. For example int double; This code is wrong because double is a keyword and you cannot use it as a variable.

List of some valid identifiers in Java
  1. MyVariable
  2. myvariable
  3. x
  4. I
  5. my_Variable
  6. _myvariable
  7. $myvariable
  8. sum_of_array
  10. dataflair123
List of some invalid identifiers in Java
  1. My Variable (it contains a space)
  2. 123gkk (it begins with numbers)
  3. a+c (plus sign is not an alphanumeric character)
  4. variable-2 (the hyphen is not allowed)
  5. sum_&_difference (ampersand is not an alphanumeric character)
  6. O’Reilly (the apostrophe is not an alphanumeric character)
Rules for defining an Identifier in Java:
  1. 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 a compilation error.
  2. Identifiers can’t start with digit i.e., 123name is not valid.
  3. 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.
  4. There is no length limit for java identifiers but it is not recommended to take too length identifiers.
  5. We can’t use reserved words as identifiers otherwise we will get a compilation error. i.e., int if = 20; here we will get compilation error.
  6. 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.
public class Test {
   public static void main(String[] args) {
      	int a = 100;
      	System.out.println("Hai this is Ashok");

In the above example, we have 5 Identifiers: Test, main, String, args, a.

Reserved Words in Java:

The Reserved Words in Java are pre-defined for some special purpose and you cannot create a variable, class name, or method name with these names. It is used to represent functionality in programs. They can be briefly categorized into two parts: keywords(50) and literals(3).

Identifiers and Reserved Words in Java

Keywords: It defines functionality.

Literals: It is used to define values.

List of Java Keywords:
abstract assert boolean break byte
case catch char class const
continue default do double else
enum extends final finally float
for goto if implements import
instanceOf int interface long native
new package private protected public
return short static strictfp super
switch synchronized this throw throws
transient try void volatile while

Instead of these keywords, you cannot use true, false, and null as identifiers because these are Literals. To know more about literals, Please read our Java Literals article.

Note: The keywords const and goto are reserved, even though they are not currently used. In place of const, the final keyword is used. Some keywords like strictfp are included in later versions of Java.

In the next article, I am going to discuss Control Flow Statements in Java with Examples. Here, in this article, I try to explain Identifiers and Reserved Words in Java with Examples and I hope you like this Identifier and Reserved Words in Java with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *