java programming symbol imprint on green sea yellow sand beack background

1.5 Java | Identifiers

4. Explaining How Identifiers Work in Java

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:

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?