Table of Contents
Java Identifiers
As the name suggests, Java identifiers are used to essentially identify the code and break it down to several parts. This greatly enhances the readability of the code. A Java identifier can be anything starting from class names to variables names or any such labels that demarcate segments of code.
Let’s take the example of a simple code below:
public class Demo { public static void main (String args []) { double db = 20.0; } }
The above code has the following identifiers:
- Demo: Name of the class.
- main: Name of the method.
- String: Library Class name.
- args: Name of a variable of String type.
- db: Name of the double variable we have chosen.
Rules for Defining Java Identifiers
Java has defined some rules when naming identifiers. Violating these rules result in compilation errors:
- Only alphanumeric characters are allowed when naming an identifier. ( [A – Z], [a – z], [0 – 9]), ‘$’ (dollar sign) and ‘_’ (underscore). Any other special character will not be accepted when compiling your code.
- The sequence of identifiers is such that it should always start with a character. It can then be followed by digits but never vice versa.
- Java identifiers are case-sensitive.
- Although identifiers can be as long as you want, an optimal length is anything between 4 to 15 letters.
- Reserved words cannot be used as identifiers.
Examples of Valid Identifiers:
TestA
TESTA
testa
x
x1
_testA
$testA
Examples of Invalid Identifiers:
Test A //Cannot contains space
123A //Should not start with letters
x+y //’+’ is not acceptable.
Reserved Words
Reserved Words are those words that have special meaning in the gambit of the programming language of Java. Reserved words are formed of 50 keywords and 3 literals. These words cannot be used as identifiers.
0 Comments