Back to: Java Tutorials For Beginners and Professionals
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
- MyVariable
- myvariable
- x
- I
- my_Variable
- _myvariable
- $myvariable
- sum_of_array
- MYVARIABLE
- dataflair123
List of some invalid identifiers in Java
- My Variable (it contains a space)
- 123gkk (it begins with numbers)
- a+c (plus sign is not an alphanumeric character)
- variable-2 (the hyphen is not allowed)
- sum_&_difference (ampersand is not an alphanumeric character)
- O’Reilly (the apostrophe is not an alphanumeric character)
Rules for defining an Identifier in Java:
- 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.
- 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 a 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.
Example:
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).
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.