Java Identifiers
In Java, the symbolic notations used for identification are called identifiers. Identifiers can be the name for the class, variable name declaration, name of the package, constant name and many others. However, several words in Java are reserved and cannot be used as identifiers.
Before specifying an identifier, some rules should be followed. Let's explain it using a small Java code:
public class HelloEveryone {
public static void main(String[] args) {
System.out.println("Hello Everyone");
}
}
We have the corresponding Java identifiers as from the example above:
1. HelloEveyone is the class name
2. main (main method)
3. String (the name for a predefined class)
4. args ( variables for String)
5. System ( Predefined class)
6. out (Variable name)
7. println (method of returning output)
Let's study the Java identification instructions:
Rules for Identifiers in Java
When specifying identifiers in Java, there are a few guidelines and practices to follow. It's possible to get a compile-time error if the identifiers are not properly specified. The guidelines and conventions for declaring identifiers are as follows:
- The characters [A-Z] or [a-z] or [0-9] and an underscore (_) or a dollar sign ($) are required for a valid identification. For instance, the identifier @java is invalid because it contains the special letter @.
- An identification shouldn't contain any empty spaces. For instance, the java programming identifier is incorrect because it contains space.
- A number at the beginning of identification is not allowed. For instance, the identification 123java is incorrect.
- A unique identification should only be 4 to 15 letters long. There is no length restriction, although. However, it is important to follow accepted rules.
- Java reserved keywords like int, float, double, char, etc. cannot be used as identifiers. For instance, in Java, int double is an unnecessary identifier.
- The keywords from the sql database, like SELECT, FROM, COUNT, DELETE, etc., shouldn't be used as an identifier.
Java Reserved Keywords
Java reserved keywords were words that have already been defined and are reserved for any purpose. These keywords cannot be used as identifier names for classes or methods. Java's syntax makes use of these keywords for a variety of purposes. It will produce an error if we utilise a reserved word in the name of the variables.
Every reserved keyword in java has its meaning and purpose.
Example:
double height:
Some of the reserved keywords are:
Byte, short, int, long, double, for, while, if, else, Try, throw, throws, private, public, protected, Static, etc.
Let us look at some of the valid and invalid identifiers.
Valid identifiers
Below are some of the valid identifiers:
- TestStatistics
- Variables
- d
- i
- Java_Lab
- $teststatistics
- TEAM
- jupyter123
- Java123
Invalid identifiers
Below are some of the invalid identifiers:
- Test Statistics (space is not included)
- 123java ( it should not contain the number with the beginning)
- java+program ( + the symbol can’t be used in the identifier)
- java-program ( the symbol(-) is not allowed)
- java_&_programming( address symbol is not allowed in an identifier)
- java’programs ( the apostrophe symbol can’t be used in the middle)
While we are declaring an identifier, it must be according to the naming conventions.
But, the Java programming language does not require that you conform to these rules.
Without following these rules, the code becomes more complex.