Updated March 31, 2023
Definition of Java Identifiers
In Java, identifiers are considered as a sequence of 1 or more than 1 character that helps in naming variables, methods, classes, etc. In order to create an identifier, there are certain rules. In addition to that, certain character sequences such as keywords, reserved words, literals can’t be used as identifiers since they have a predefined meaning in Java. Let us see the basic rules of creating an identifier in the next section.
Rules for Java Identifiers
As already mentioned, Java identifiers has to follow the same rules for creation. If they are not followed, compile-time errors can occur. The rules are explained below.
- Rule #1: Identifier can’t be same as Reserved words. These reserved words can be keywords or literals. Following are the keywords that are available in Java.
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 |
Here, eventhough const and goto are not part of the Java language, they are considered as keywords.
- Rule #2: Identifiers can’t be words such as null, true and false as they are literals.
- Rule #3: Identifiers are considered as case sensitive. Moreover, certain other rules are on using the cases in certain situations even though the compilers do not force it. That is, instead of using _(underscore), Java prefers to use CamelCase, where the first letter of two successive words will be capitalized.
- In the case of Interface names and class names, names start with letters of uppercase and follow in lower case. As already mentioned, if multiple words are used, then CamelCase has to be followed.
Example: SampleClass, Employee
At the same time, in the case of Variable names and method names, the lower case is followed. Similar to the above situation, the camel case will be followed if multiple words are used.
Example: number,MyNumber
In the case of Constants, it is advised to use all uppercase or use _(underscore) to separate words.
Example: BOOLEAN
- Rule 4: Even though Identifiers can contain digits [0-9], letters [A-Z] [a-z] etc., it should start with a letter or symbols such as $(dollar) or _(underscore). In any case, it should not start with a digit.
- Rule 5: White space is not permitted in identifiers.
- Rule 6: Symbols such as @,# are not permitted.
- Rule 7: Since ?(question mark) is a reserved word, it can’t be used as an identifier.
- Rule 8: Even though Identifier length do not have any limit, it is advised that identifiers should have an optimal length of 4-15.
Examples of Invalid identifiers’ and its reason.
Invalid Identifier | Reason why it is invalid |
Try | try is a keyword. [Rule 1] |
Null | Null is one of the literals in Java. [Rule 2] |
456Anna | Identifiers should not start with a digit. [Rule 4] |
happy@ | Since @ is a special character, it can’t be used. [Rule 6] |
num? | Since ? is a reserved word, it can’t be used. [Rule 7] |
num 1; | Identifiers should not contain white space. [Rule 5] |
Java Identifiers with Examples
Normally, many people consider identifiers as variables only. But the true fact is that an identifier can be a class name, package name, method name etc. For example, let us see the below code.
public static void main(String args[]) {
// variable declaration
int number = 13;
Here, each and every word in the code is considered as an identifier. But as our rule 1 says, keywords can’t be used as an identifier. It is due to the fact that keywords and literals are already predefined.
Suppose a program defines a keyword as an identifier, as shown below, and we are trying to compile it. What will happen?
public class JavaIdentifierExampl {
//main method
public static void main(String args[]) {
// variable declaration
int for = 13;
System.out.println("value of the number variable is : "+ for);
}
}
Output:
In the above sample output, an exception has occurred. It is because of the usage of keyword for in the program.
At the same time, let us use a predefined method name main in the above program instead of for.
//Java program with an identifier which do not have any whitespace
public class JavaIdentifierExampl {
//main method
public static void main(String args[]) {
// variable declaration
int main = 13;
System.out.println("value of the number variable is : "+ main);
}
}
Output:
SO as you can see, there is no error in executing the code. Because Identifiers can be predefined method names, class names, etc., but predefined keywords and literals can’t be used in the same way.
Now, let us see some of the valid identifiers and java programs based on that.
Example #1
Java program with an identifier that is not the keyword or literal.
Code:
//Java program with an identifier which is not keyword or literal
public class JavaIdentifierExampl {
//main method
public static void main(String args[]) {
// variable declaration
int number = 25;
System.out.println("value of the number variable is : "+number);
}
}
Output:
Example #2
Java program with an identifier which do not have any whitespace.
Code:
//Java program with an identifier which do not have any whitespace
public class JavaIdentifierExampl {
//main method
public static void main(String args[]) {
// variable declaration
int number_23 = 125;
System.out.println("value of the number variable is : "+number_23);
}
}
Output:
Example #3
Java program with an identifier that starts with $.
Code:
//Java program with an identifier that starts with $
public class JavaIdentifierExampl {
//main method
public static void main(String args[]) {
// variable declaration
int $number_23 = 20;
System.out.println("value of the number variable is : "+ $number_23);
}
}
Output:
Recommended Articles
This is a guide to Java Identifiers. Here we also discuss the definition and rules for java identifiers along with different examples and their code implementation. You may also have a look at the following articles to learn more –