An identifier is a name that will be used to describe classes, methods, constants, variables; anything a programmer is required to define.
The rules for naming identifiers in Java are:
- Identifiers must begin with a letter.
- Only letters, digits, or an underscore may follow the initial letter.
- The blank space cannot be used.
- Identifiers cannot be reserved words. Reserved words or keywords are already defined in Java. These include words such as new, class, int, etc. See Handout A3.1, Reserved Words in Java
Java is a case sensitive language. That is, Java will distinguish between upper and lower case letters in identifiers. Therefore:
grade
and Grade
are different identifiers
Be careful both when naming identifiers and when typing them into the code. Be consistent and don’t use both upper and lower case names for the same identifier.
- A good identifier should help describe the nature or purpose of whatever it is naming. For a variable name, it is better to use
grade
instead of g
, number
instead of n
.
However, avoid excessively long or "cute" identifiers such as:
gradePointAverageForStudentsAToZ
or bigHugeUglyNumberThatIsPrettyPointlessButINeedItAnyway
Programmers will adopt different styles of using upper and lower case letters in writing identifiers. The reserved keywords in Java must be typed in lower case text, but identifiers can be typed using any combination of upper and lower case letters.
The following conventions will be used throughout this curriculum guide:
- A single word identifier will be written in lower case only. Examples:
grade
, number
, sum
.
- Class names will begin with upper case. Examples: String, DrawingTool, SketchPad, Benzene.
- If an identifier is made up of several words, all words beyond the first will begin with upper case. Examples:
stringType
, passingScore
, largestNum
, DrawHouse
, SketchPad
.
- Identifiers used as constants will be fully capitalized. Examples:
PI
, MAXSTRLEN
.