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.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
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.
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
Examples of Invalid identifiers’ and its reason.
|
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] |
public static void main(String args[]) { // variable declaration int number = 13;
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.
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.
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); } }
Suppose a program defines a keyword as an identifier, as shown below, and we are trying to compile it. What will happen?
Output:
//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); } }
At the same time, let us use a predefined method name main in the above program instead of for.
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.
Example #1
Java program with an identifier that is not the keyword or literal.
//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.
//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 $.
//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:
The above is the detailed content of Java Identifiers. For more information, please follow other related articles on the PHP Chinese website!