Everything You Must Know About Java Identifiers

Talking about the beginning level, every program consists of variables, methods, and classes. A program is pointless if it doesn’t contain class, method, and variable. To declare any variable, we declare the data-type of the variable and then Variable Name.

We have discussed variables, classes, and methods in our earlier articles. Every language has some rules and constraints that we need to adopt. For example, in English, Tenses are the rules which define the structure and How to talk and write the language.

Similarly, all programming languages also have some rules and the constraints that one has to follow if we write the code in that particular language. Identifiers also define some constraints in the Java programming language. Identifiers are named to identify something in a program.

Let’s quickly jump to the Identifiers in Java and understand all types of Java Identifiers.

Java Identifiers

An Identifier is simply a sequence of characters. In other words, we can say, an Identifier is just a word or a single character that is used in our program. If the identifier is named according to the rules then only it is considered to be valid otherwise the compiler will generate an error.

The most important point that any programmer has to understand is that the first character of an identifier is very important. The validation depends on the first character of the identifier.

Java programming language allows letters (A-Z, a-z), $, and _ in the starting of any identifier. Other than these characters, no other special symbols and numbers are allowed at the starting of any identifier. After the first letter, Java programming language support digits (0-9), letters (A-Z, a-z), $, and _ to declare any identifier.

Java Identifier Example:

public class Main {
  public static void main(String[] args) {
    String str = "Techvidvan";
    int num = 10;
    float fl = 12.0;
  }
}

In the above code snippet, the identifiers are:

Main: The name of the class that was user defined identifier.
main: The name of the method that was defined by the user.
String: The class name that is reserved in Java Programming Language.
args: The variable with data-type as String.
str: Another String type variable holding some string type value.
num: The variable with data-type int and holding some integer value.
fl: The variable with data-type float and holding some floating value.

Rules for Defining Identifiers in Java

There are some rules that we have to keep in mind before declaring any identifier in the Java programming language. These rules are also applicable to the C and C++ languages.

1. An Identifier can consist of characters ([A-Z], [a-z], [0-9]), ‘$‘(dollar sign) and ‘_‘ (underscore) as its first character. Java allows digits (0-9) for a non-first character in any identifier.

For example, “techvidvan#” is an invalid name of the variable as a special character “#” is present in it which is not allowed in the identifier as per the rules of the Java Programming language.

2. The identifiers should not begin with digits ([0-9]).

For example, “123techvidvan” is invalid.

3. The identifier can be of any length. Still, programmers are advised to use them for the appropriate length of 4-15 letters.

4. There are a total of 51 keywords among which 2 are unused (goto and const) and Java does not recommend to use them. These 49 keywords are reserved and we can’t use them as identifiers. If we use the keywords as identifiers then we will get an error.

Some reserved keywords are public, import, short, try, new, switch, final, extends, class, etc.

For example, if we write, int try = 5; then this line will give an error as try is an invalid identifier.

5. There should be no white-space while declaring any identifier. Java Programming language doesn’t support white-space between any identifier.

For Example, a variable with the name “tech vidvan” is not a valid variable in java.

6. The declaration of all the variables is case-sensitive i.e. Java programming language distinguishes the upper case and lowercase letter also.

For example, variables with the name “Myvar” and with the name “myvar” are totally different from each other.

Note: Java is case-sensitive as it treats uppercase and lowercase characters differently.

Valid Identifiers in Java

An identifier is valid if and only if it follows the rules for defining it. Below are some identifiers that are valid in Java:

MyVar
a
myvar
MYVAR
j
_myvar
$myvar
series_of_numbers
techvidvan123

Invalid Java Identifiers

The identifier that does not follow the rules are invalid identifiers in Java. Some invalid identifiers in Java are:

DATA-REC             contains special character – (other than A-Z, a-z, and _ or $)
98MyVar                Starting with a digit
break                      reserved keyword
My.file                    contains special character

Java Identifier’s Naming Conventions

While naming identifiers, we should follow certain conventions which are:

1. The names of public methods and instance variables should begin with a lowercase letter.

For example,

                                   method: sum()
Instance variable: number

2. For names with multiple words, second and subsequent words beginning character is capital so as to enhance the readability.

For example,

avgSalaryOfEmployee, dateOfBirth, getNumberOfStudents, etc

3. Private and local variables should always be lowercase.

For example,

width, result,final_score, etc

4. The class names and interface names should begin with an uppercase letter.

For example,

MyClass, Employee, ChildClass, Student, etc.

5. Naming of all the constants should be using all capital letters and underscores.

For example,

MAX_VALUE, MAX_MARKS, SPECIL_SALARY, TOTAL etc.

Code to understand the types of Variables in Java:

package com.techvidvan.identifiers;
public class TypesOfVariable {
  public static void main(String args[]) {
    //Declaring all types of variables.
    int MyVar = 1;
    int MYVAR = 12;
    int myvar = 123;
    int a = 1234;
    int j = 12345;
    int _myvar = 123456;
    int $myvar = 1234567;
    int series_of_numbers = 12345678;
    int techvidvan123 = 123456789;

    // Printing all the variables
    System.out.println("Assigned value of the variable MyVar: " + MyVar);
    System.out.println("Assigned value of the variable MYVAR: " + MYVAR);
    System.out.println("Assigned value of the variable myvar: " + myvar);
    System.out.println("Assigned value of the variable a: " + a);
    System.out.println("Assigned value of the variable j: " + j);
    System.out.println("Assigned value of the variable _myvar: " + _myvar);
    System.out.println("Assigned value of the variable $myvar: " + $myvar);
    System.out.println("Assigned value of the variable series_of_numbers: " + series_of_numbers);
    System.out.println("Assigned value of the variable techvidvan123: " + techvidvan123);
  }
}

Output:

Assigned value of the variable MyVar: 1
Assigned value of the variable MYVAR: 12
Assigned value of the variable myvar: 123
Assigned value of the variable a: 1234
Assigned value of the variable j: 12345
Assigned value of the variable _myvar: 123456
Assigned value of the variable $myvar: 1234567
Assigned value of the variable series_of_numbers: 12345678
Assigned value of the variable techvidvan123: 123456789

Conclusion

Identifiers in Java is one of the basic fundamentals of Java that is mandatory for any Java learner. Without learning the identifiers, its rules and naming convention, you can’t efficiently program in Java.

The naming conventions are optional, but you should follow them as a rule so that it increases the readability of the code. If you write the code by following these conventions, then anyone who further reads or edits the code can easily understand the meaning and intentions of all the identifiers like classes, variables, methods, etc.

We hope you enjoyed learning this article of identifiers in Java.