Identifiers in Java with Examples

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

Identifiers in Java are names that are helpful in uniquely recognizing a class, a method, a package name, a constant, or a variable. Some words in Java are reserved and cannot be used as identifiers. Certain rules must be followed in Java while we are defining an identifier, or the compiler will throw an error.

See Also: Complete Java Tutorial

Introduction to Identifiers in Java

In general terms, an identifier is a name given to an entity for identification. Java identifiers are any attribute/property of an entity that is utilized for identification. For example, a person's name, an employee's employee number, or an individual's social security number.

Identifiers in Java are names that distinguish between different Java entities, such as classes, methods, variables, and packages. Identifiers include the names of classes, methods, variables, packages, constants, etc. These identifiers are each specified using a specific syntax and naming scheme.

The syntax would depend on the type of identifier. For example, an integer variable in Java can be declared as below,

int <variable name(identifier)>;

The variable name is the name given by the programmer to uniquely identify the int variable. Later the programmer can use that variable in his program. Hence, here the variable name is the identifier. These Identifiers follow a certain naming convention/rules when they are named, which we shall look at in the next section of this article.

Example:

Example Java Code Snippet

Identifiers: Below is the list of identifiers that are present in the above sample code.

  • MainClass (Class name)
  • main (Method name)
  • String (Predefined Class name)
  • args (String variable name)
  • var1 (integer variable name)
  • var2 (double variable name)
  • System(Predefined Class name)
  • out(Variable name)
  • println (Method name)

Rules for Identifiers in Java

Below are the rules that need to be followed when defining an identifier in Java. A compile-time error will be produced if any of the following rules are broken.

Rule 1:

Identifiers can only contain alphanumeric characters [a-z] [A-Z] [0-9] , dollar sign ($) and underscore ( _ ). No other character is allowed. Valid Examples:

IdentifierExplanation
NomansLand90This is valid as it contains only alphanumerics
$Wink1NTomBoyThis is valid as characters are alphanumerics, and $

Invalid Examples:

IdentifierExplanation
wiseWizard#1994This is invalid as # character is not allowed

Rule 2:

Identifiers cannot start with a numeric value [0-9]. The starting character should be alphabet [A-Z] [a-z], dollar ($) or underscore ( _ ).

Valid Example:

IdentifierExplanation
ScalerAcademyThis is valid as the name starts with an alphabet.

Invalid Example:

IdentifierExplanation
1ScalerAcademyThis is invalid as the name starts with a numeric value 1.

Rule 3:

Identifiers should not contain spaces in their name.

Valid Example:

IdentifierExplanation
Four_HorseMen$2021This is valid as characters are alphanumerics, $

Invalid Example:

IdentifierExplanation
Four HorseMen$2021This is invalid as there is space after Four

Rule 4:

Java identifiers are case-sensitive.

Eg: ‘Vendetta’ and ‘vendetta’ are considered as two different identifiers.

Rule 5:

The standard convention sets the size of the Java identifiers between 4 – 15, and it is advised to follow that length when defining an identifier. However, there is no upper limit on the length of an identifier.

Rule 6:

Reserve Keywords cannot be used as Identifiers. Java defines a total of 53 reserved keywords that are predefined. In the following part, we'll examine what a reserved keyword is and a list of predefined terms.

Example: int, float, public, static, etc.

Java Reserved Keywords

Reserved keywords are keywords that are used by java syntax for a particular functionality. Since each of these reserved keywords' functionality is predefined, these keywords cannot be used for any other purpose.

Java predefines a set of 53 reserved keywords that cannot to used as Identifiers. So these keywords cannot be used as Class names, Method names, Package names, or Variable names. We have some of these keywords in our sample program in the introduction. int, double, public, static, etc., are all examples of reserved keywords.

Example 1: int Var1; → Here, int is a reserved keyword that is used to define an integer variable. The variable name is Var1, which is an identifier for an int variable.

Example 2: int break; → Here, int is a reserved keyword, and the variable name is break, which is invalid as break itself is a reserved keyword with its predefined functionality in java. Hence this statement will throw an error.

Below is the table of references for all the 53 keywords in Java:

Keywords
abstractdefaultgotopackagethis
assertdoifprivatethrow
booleandoubleimplementsprotectedthrows
breakelseimportpublictransient
byteenumintreturntrue
catchextendsinterfaceshorttry
charfalseinstanceofstaticvoid
classfinallongstrictfpvolatile
constfinallynativesuperwhile
continuefloatnewswitch
casefornullsynchronized

Valid Identifiers in Java

Below is an example list of valid Identifiers that can be used in java

IdentifierExplanation
Employeealphabets
EMP12alphanumerics
$Manager1alphanumerics and $
_AngryMonk404alphanumerics and _
Student36Pro9alphanumerics
Aalphabet uppercase A
ialphabet lowercase i
$Symbol $
final_result_valuealphabets and _
SevenUp___7alphanumerics and _

Invalid Identifiers in Java

Below is an example list of Identifiers that are invalid in java.

IdentifierExplanation
@Employeecontains invalid character @
12EMPStarts with numerics
&Manager1contains invalid character &
^AngryMonk404contains invalid character ^
36-StudentPro9Starts with numerics and contains invalid character –
5Is a numeric
final result valueContains spaces

Examples

Let's discuss few examples in detail related to Java identifiers to get a better understandin:

Valid identifier example:

Output

Explanation: In this example, myVariable is a valid identifier for an integer variable. It is assigned the value 1010, and then its value is printed, resulting in the output of 1010.

Invalid identifier example

Output/Error

Explanation: In this example, the identifier 123abc123abc violates the naming rule as it starts with a digit. Identifiers must start with a letter, underscore, or dollar sign, not a digit.

Using the reserved word as an identifier

Output/Error

Explanation: In this example, the identifier class is a reserved word in Java and cannot be used as an identifier. Attempting to use a reserved word as an identifier will cause a compilation error.

Valid constant identifier example

Output

Explanation: In this example, PIPI is a valid constant identifier representing the mathematical constant pi. It is declared as final to indicate that its value cannot be changed. The value of PIPI is then printed, resulting in the output of 3.14159.

Valid method identifier example

Output

Explanation: In this example, "printMessage" is a valid identifier for a method. It takes a String parameter "message" and prints it. The method is called with the argument "Hello, world!", resulting in the output of Hello, world!.

FAQs

Q. What are identifiers in Java?

A Identifiers in Java are names used to uniquely identify classes, methods, package names, constants, and variables.

Q. Can reserved words be used as identifiers in Java?

A No, reserved words in Java cannot be used as identifiers. Attempting to do so will result in a compilation error.

Q. What happens if an identifier violates the naming rules in Java?

A If an identifier violates the naming rules in Java, the compiler will throw an error, and the code will not compile. It is important to follow the naming rules to avoid compilation issues.

Q. Why is it important to choose meaningful identifiers in Java?

A Choosing meaningful identifiers improves code readability and maintainability. It makes the code easier to understand for developers and helps in avoiding naming conflicts.

Conclusion

  • Below are some of the main points to keep my mind when declaring identifiers in java

    • Only alphanumeric, $, and _ are valid characters for defining an identifier.
    • Do not start an identifier with a number.
    • Identifier should not contain white spaces in between.
    • Keywords should not be used as identifiers.
  • Java Identifiers are invalid if it is found to violate any of the rules of identifier (refer to section ‘Rules for Identifier in Java’), and they would generate a compile-time error.

  • Apart from the identifier naming rules, which are mandatory, Java also defines a set of naming conventions for variable names, class names, method names, etc. The primary purpose of these conventions is to make the code readable and easily understandable and to write code that conforms to industry standards. The naming conventions are not mandatory but are advised to follow.

Read Also