java programming symbol imprint on green sea yellow sand beack background

1.5 Java | Identifiers

Identifiers are the names that identify elements like classes, methods, and variables in a program.

For example; java, util, Scanner, Fnumout, main, String, args, System, out, println, Scanner, in, fnumber, input, nextDouble, etc. are all identifiers, or the names of things that appear in the program example below:

import java.util.Scanner;
public class Fnumout {
    public static void main(String[] args) {
        //Prompt the user to enter a floating point number
        System.out.println("Please enter a floating point number");

        //Make a Scanner Object, which is stored in the input variable
        Scanner input = new Scanner(System.in);

        //read the floating point number off the keyboard, and store that value in the variable fnumber
        double fnumber = input.nextDouble();

        System.out.println("This is the floating point number that you typed on the keyboard " + fnumber);
    }
}

Rules for Identifiers:

  • So, an identifier is allowed to be a sequence of character that consist of letters, digits, underscores _ and dollar signs $.
  • An identifier must start with a letter, underscore _ or dollar sign $. It cannot start with a digit.
  • An identifier cannot be a reserved keyword.
  • An identifier cannot be true, false, or null.
  • An identifier can be of any length.

For example, $2, Compute Area, area, radius, and print are legal identifiers; whereas 2A and d+4 are not legal- they do not follow the rules. The Java compiler detects illegal identifiers and reports them as syntax errors.

Note that since Java is case sensitive, area, Area, and AREA are all different identifiers.

Also, you should make identifiers descriptive so that the program is easy to read. ie. use NumberOfStudents over something vague like numStuds. However, its conventionally correct to occasionally use variable names such as i, j, k, x, and y for brevity.

Conventionally, you should not name identifiers with $ character, because by convention the $ character should only be used in mechanically generated source code.


◄◄◄BACK | NEXT►►►

What's Your Opinion?