Home > Java > javaTutorial > Java Identifiers

Java Identifiers

王林
Release: 2024-08-30 15:18:45
Original
1114 people have browsed it

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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]
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
public static void main(String args[]) {
// variable declaration
int number = 13;
Copy after login

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);
}
}
Copy after login

Suppose a program defines a keyword as an identifier, as shown below, and we are trying to compile it. What will happen?

Output:Java Identifiers


//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);
}
}
Copy after login
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.

Output:Java Identifiers

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.

//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);
}
}
Copy after login
Code:

Output:Java Identifiers

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);
}
}
Copy after login
Code:

Output:Java Identifiers

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);
}
}
Copy after login
Code:

Output:Java Identifiers

The above is the detailed content of Java Identifiers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template