Java Reference
In-Depth Information
Naming Your Variables
The name that you choose for a variable, or indeed the name that you choose for anything in Java, is called
an
identifier
. An identifier can be any length, but it must start with a letter, an underscore (_), or a dollar sign
($). The rest of an identifier can include any characters except those used as operators in Java (such as +, −,
or *), but you are generally better off if you stick to letters, digits, and the underscore character.
Java is case sensitive, so the names
republican
and
Republican
are not the same. You must not include
blanks or tabs in the middle of a name, so
Betty May
is out, but you could have
BettyMay
or even
Betty_May
. Note that you can't have
6Pack
as a name because you cannot start a name with a numeric digit.
Of course, you could use
sixPack
as an alternative.
Subject to the restrictions I have mentioned, you can name a variable almost anything you like, except
for two additional restraints: you can't use a
keyword
as a name for something, and a name can't be anything
that could be interpreted as a constant value — as a literal in other words. Keywords are words that are part
of the Java language. You saw some keywords in the previous chapter, and you will learn a few more in this
chapter. If you'd like to know what they all are now, see the complete list in Appendix A. The restriction on
constant values is there because, although it is obvious why a name can't be 1234 or 37.5, constants can also
be alphabetic, such as
true
and
false
, which are literals of the type
boolean
. Of course, the basic reason
for these rules is that the compiler has to be able to distinguish between your variables and other things that
can appear in a program. If you try to use a name for a variable that makes this impossible, then it's not a
legal name.
Clearly, it makes sense to choose names for your variables that give a good indication of the sort of data
they hold. If you want to record the size of a hat, for example,
hatSize
is a good choice for the variable
name whereas
qqq
would be a bad choice. It is a common convention in Java to start variable names with
a lowercase letter and, where you have a name that combines several words, to capitalize the first letter of
each word, as in
hatSize
or
moneyWellSpent
. You are in no way obliged to follow this convention, but
because almost all the Java world does, it helps to do so.
NOTE
If you feel you need more guidance in naming conventions (and coding conventions in
Variable Names and Unicode
Even though you may be entering your Java programs in an environment that stores ASCII characters, all
Java source code is in Unicode. Although the original source code that you create may be ASCII, it is con-
verted to Unicode characters internally, before it is compiled. Although you can write any Java language
statement using ASCII, the fact that Java supports Unicode provides you with immense flexibility. It means
that the identifiers that you use in your source program can use any national language character set that
is defined within the Unicode character set, so your programs can use French, Greek, or Russian variable
names, for example, or even names in several different languages, as long as you have the means to enter
them in the first place. The same applies to character data that your program defines.