The Java programmer needs to make up names for things such as variables, method and function names and symbolic constants (see next section). A name that he makes up is called a user identifier. There are a few simple rules to follow in naming an identifier:
• it must start with a letter (a-z or A-Z) or underscore (_) or dollar sign ($);
• if other characters are required, they can be any combination of letters, digits (0-9), underscore or dollar sign;
• there is no limit to the length of an identifier.
Examples of valid identifiers:
r
R
sumOfRoots1and2
$1000
_XYZ
maxThrowsPerTurn
TURNS_PER_GAME
R2D2
root$1$2
Examples of invalid identifiers:
2hotToHandle // does not start with a letter or _ or $
Net Pay // contains a space
ALPHA;BETA //contains an invalid character ;
$1,000 //contains an invalid character ,
Important points to note:
• Spaces are not allowed in an identifier. If you need one which consists of two or more words, use a combination of uppercase and lowercase letters (as in numThrowsThisTurn) or use the underscore to separate the words (as in num_throws_this_turn). We prefer the uppercase/lowercase combination.
• In general, Java is case-sensitive (an uppercase letter is considered different from the corresponding lowercase letter). Thus r is a different identifier from R. And sum is different from Sum is different from SUM is different from SuM.
• You cannot use a Java reserved word as one of your identifiers.