-
Giving Names to Boxes
-
Programs read and write from boxes. We number each box with a
unique number to let us know which box is which. Thus, if we have 3
boxes, we number them box 0, box 1, and box 2. The number is the box
ID.
Even though computers have few problems working with numbers,
people write programs, and people generally prefer words, since they
are more descriptive. For example, we might say one box is Samir's
salary, or another box is Anna's age.
Giving descriptive names to boxes makes it easier to
to remember what the box is used for.
Thus, we might name a box samirSalary. As far as the
computer is concerned, this may be box 2. In other words,
samirSalary is an alias or a shortcut to box 2.
As a programmer, instead of identifying the box by number, you
give each box a unique name. That way, it's much easier to
remember what each box is being used for.
-
Identifiers
-
In Java (as in many programming languages), the names of the
boxes are called variable names. The word "variable" means
changing, because we can change the value inside the box. We can't
change its name, however.
Variable names fall in the category of something known as
identifiers. Identifiers are names given to things in Java.
This includes variables, methods, classes, etc. So far, you don't
know anything about methods or classes. We'll get to them in future
lessons.
An identifier refers to names of "things" in Java.
Java has strict rules about valid identifiers. Make sure you
understand them (they aren't difficult to master) so you don't
make errors when you use them.
-
But First...
-
But first, let's define a few sets.
- alpha This is the set of alphabetic characters
{ a, b, ..., z, A, B, ... Z }. It contains the 26 uppercase
letters and the 26 lowercase letters. We call this set alpha.
- digit This is the set of digits
{ 0, 1, ..., 9 }. It contains the 10 digits from 0 to 9.
We also have underscore, which is the character '_' (the single
quotes are used to make it easier to see), and dollar sign, '$'.
Identifiers contain characters from any of: alpha, digit,
underscore, and dollar sign. You can't use spaces or tabs or
symbols like #, @, !, and so forth in an identifier.
Wait, we're not done yet. More rules are coming.
-
Syntax of an Identifier
-
Syntax is a grammatical rule. Here is the syntax for valid
Java identifiers:
- Each identifier must have at least one character.
- The first character must be picked from: alpha, underscore,
or dollar sign. The first character can not be a digit.
- The rest of the characters (besides the first) can be from:
alpha, digit, underscore, or dollar sign. In other
words, it can be any valid identifier character.
Put simply, an identifier is one or more characters selected
from alpha, digit, underscore, or dollar sign. The only
restriction is the first character can't be a digit.
-
Examples of Valid Identifiers
-
Here are some valid identifiers:
- aaa
- sales_tax
- _circleArea
- box100width
- $directory
- ab1234$$
Although you have great flexibility for identifiers, we're
going to make more restrictions for which identifiers we should
use.
But first, to look at some invalid identifiers.
-
Examples of Invalid Identifiers
-
It's easy to make a mistake and use a bad identifier.
- 1ab (ERROR: first character starts
with a digit)
- num-oranges (ERROR: dash is not permitted
in identifiers)
- num oranges (ERROR: space is not permitted
in identifiers)
-
Java Style Identifiers
-
Java has a particular way of writing variable names. This is
called Java-style. Style refers to something that is
recommended, but not required in Java. In other words, the compiler
won't complain if you break style rules. However, other programmers
may think you're using "bad" style.
For variable names, Java has the following recommended style.