Java Reference
In-Depth Information
The Java language specification defines the set of letters to include the underscore
and dollar-sign characters (_ and $), which means that the following are legal identi-
fiers as well:
two_plus_two
_count
$2donuts
MAX_COUNT
The following are illegal identifiers:
two+two
hi there
hi-There
2by4
Java has conventions for capitalization that are followed fairly consistently by pro-
grammers. All class names should begin with a capital letter, as with the Hello ,
Hello2 , and Hello3 classes introduced earlier. The names of methods should begin
with lowercase letters, as in the main method. When you are putting several words
together to form a class or method name, capitalize the first letter of each word after
the first. In the next chapter we'll discuss constants, which have yet another capitaliza-
tion scheme, with all letters in uppercase and words separated by underscores. These
different schemes might seem like tedious constraints, but using consistent capitaliza-
tion in your code allows the reader to quickly identify the various code elements.
For example, suppose that you were going to put together the words “all my chil-
dren” into an identifier. The result would be
AllMyChildren for a class name (each word starts with a capital)
allMyChildren for a method name (starts with a lowercase letter, subsequent
words capitalized)
ALL_MY_CHILDREN for a constant name (all uppercase, with words separated by
underscores; described in Chapter 2)
Java is case sensitive, so the identifiers class , Class , CLASS , and cLASs are all
considered different. Keep this in mind as you read error messages from the com-
piler. People are good at understanding what you write, even if you misspell words or
make little mistakes like changing the capitalization of a word. However, mistakes
like these cause the Java compiler to become hopelessly confused.
Don't hesitate to use long identifiers. The more descriptive your names are, the
easier it will be for people (including you) to read your programs. Descriptive identi-
fiers are worth the time they take to type. Java's String class, for example, has a
method called compareToIgnoreCase .
Be aware, however, that Java has a set of predefined identifiers called keywords that
are reserved for particular uses. As you read this topic, you will learn many
of these keywords and their uses. You can only use keywords for their intended pur-
poses. You must be careful to avoid using these words in the names of identifiers. For
example, if you name a method short or try , this will cause a problem, because
short and try are reserved keywords. Table 1.4 shows the complete list of reserved
keywords.
 
Search WWH ::




Custom Search