Java Identifier
Identifiers in java :-
Identifiers in Java are symbolic names used for identification.
They can be a class name, variable name ( local variables, instance and class variables), method name,
package name, lable name , constant name, and more.
However, In java There are some reserved words that can not be used as an identifier.
For every identifier there are some conventions that should be used before declaring them.
Let's understand it with a simple Java program :
public class HelloJava {
public static void main(String[] args) {
System.out.println("Hello JavaTpoint");
}
}
From the above example, we have the following Java identifiers:
1. HelloJava (Class name)
2. main (main method)
3. String (Predefined Class name)
4. args (String variables)
5. System (Predefined class)
6. out (Variable name)
7. println (method)
Rules for Identifiers in Java :-
There are some rules and conventions for declaring the identifiers in Java.
If the identifiers are not properly declared, we may get a compile-time error.
Following are some rules and conventions for declaring identifiers:
A valid identifier must have characters [A-Z] or [a-z] or numbers [0-9], and underscore(_) or a dollar
sign ($). for example, @javatpoint is not a valid identifier because it contains a special character which
is @.
There should not be any space in an identifier. For example, java tpoint is an invalid identifier.
An identifier should not contain a number at the starting. For example, 123javatpoint is an invalid
identifier.
Java Identifier 1
An identifier should be of length 4-15 letters only. However, there is no limit on its length. But, it is
good to follow the standard conventions.
We can't use the Java reserved keywords as an identifier such as int, float, double, char, etc. For
example, int double is an invalid identifier in Java.
All identifiers can begin with a letter, a currency symbol or an underscore (_). According to the
convention, a letter should be lower case for variables.
Most importantly identifiers are case-sensitive.
An identifier cannot be the same as a query language keyword. Here is a list of query language
keywords:
ABS ALL AND ANY AS
ASC AVG BETWEEN BIT_LENGTH BOTH
BY CASE CHAR_LENGTH CHARACTER_LENGTH CLASS
COALESCE CONCAT COUNT CURRENT_DATE CURRENT_TIMESTAMP
DELETE DESC DISTINCT ELSE EMPTY
END ENTRY ESCAPE EXISTS FALSE
FETCH FROM GROUP HAVING IN
INDEX INNER IS JOIN KEY
LEADING LEFT LENGTH LIKE LOCATE
LOWER MAX MEMBER MIN MOD
NEW NOT NULL NULLIF OBJECT
OF OR ORDER OUTER POSITION
SELECT SET SIZE SOME SQRT
SUBSTRING SUM THEN TRAILING TRIM
TRUE TYPE UNKNOWN UPDATE UPPER
VALUE WHEN WHERE
Best Practices for Naming :-
Some widely recognized best practices for naming in Java are given below.
1. Use Meaningful Names: Choose names that clearly and intuitively describe the purpose of the variable,
method, or class. For example, use employeeSalary instead of vague names like es or data.
2. Follow Naming Conventions: Adhere to Java's standard naming conventions -
Classes and Interfaces: Use CamelCase with the first letter capitalized, e.g. Student, BankAccount.
Methods and Variables: Use lowerCamelCase, e.g. calculateSalary, firstName.
Java Identifier 2
Constants: Use all uppercase letters with underscores to separate words,
e.g. MAX_SIZE, DEFAULT_VALUE.
3. Start with a Letter: Although underscores and dollar signs are allowed, it's standard practice to start
names with a letter.
4. Avoid Using Single Characters: Except for temporary and loop variables (like i, j, x, y), avoid single-
character names. Descriptive names make the code more understandable.
5. Be Consistent: Use consistent naming patterns throughout your codebase. This consistency helps others
understand the structure and purpose of your code more quickly.
6. Avoid Abbreviations and Acronyms: Avoid using abbreviations unless the abbreviation is more widely
known than the full word (like URL). They can make the code less readable, especially for those unfamiliar
with the abbreviations.
7. Differentiate Clearly: Avoid using names that differ only by number or case, e.g. user1, user2,
or User, user, as they can be easily confused.
8. Use Pronounceable Names: Names that can be pronounced are generally easier to discuss, which can
be important during team discussions or code reviews.
9. Context Matters: Use names that provide enough information about their use. For
instance, saveEmployeeData is more informative than just saveData.
10. Avoid Redundancy and Noise Words: Avoid redundant information that doesn’t add clarity,
e.g. Customer class doesn’t need variables like customerName. Just the name is sufficient.
Valid Identifiers in Java :-
Below is an example list of valid Identifiers that can be used in java
Identifier Explanation
Employee alphabets
EMP12 alphanumerics
$Manager1 alphanumerics and $
_AngryMonk404 alphanumerics and _
Java Identifier 3
Student36Pro9 alphanumerics
A alphabet uppercase A
i alphabet lowercase i
$ Symbol $
final_result_value alphabets and _
SevenUp___7 alphanumerics and _
Invalid Identifiers in Java :-
Below is an example list of Identifiers that are invalid in java.
Identifier Explanation
@Employee contains invalid character @
12EMP Starts with numerics
&Manager1 contains invalid character &
^AngryMonk404 contains invalid character ^
36-StudentPro9 Starts with numerics and contains invalid character –
5 Is a numeric
final result value Contains spaces
DURGA SIR :-
All predefined java class name and interface names we can use as identifiers.
Ex :- int String = 888;
int Runnable = 777;
It is valid , but it is not programming practise , because it reduce redability and create confusion.
Java Identifier 4