Programming and Problem Solving with Java

(やまだぃちぅ) #1
2.1 The Elements of Java Programs | 47

in Java), or a place in the computer’s memory that holds data (called a fieldin Java). Identifiers
are made up of letters (A–Z, a–z), digits (0–9), the underscore character (_), and the dollar
sign ($), but each mustbegin with a letter, underscore, or dollar sign:


Identifiers beginning with an underscore have special meaning in some Java sys-
tems, so it is best to begin an identifier with a letter. Similarly, the dollar sign has
special meaning in some Java systems and should not be used in identifiers that
you write. We have included it in the syntax template so that you can recognize
its use if you encounter it in Java code that someone else has written.
Here are some examples of valid identifiers:


sum_of_squares J9 box_22A GetData Bin3D4 count Count


Note that the last two identifiers (countand Count) are considered completely dif-
ferent names by the Java compiler. That is, the uppercase and lowercase forms of a letter are
two distinct characters to the computer. Here are some examples of invalid identifiers and
the reasons why they are invalid:


Invalid Identifier Explanation
40Hours Identifiers cannot begin with a digit
Get Data Blanks are not allowed in identifiers
box–22 The hyphen (–) is a math symbol (minus) in Java
empty_? Special symbols such as? are not allowed
int The word intis predefined in the Java language

The last identifier in the table,int, is an example of a reserved word. Reserved words have
specific uses in Java; you cannot use them as programmer-defined identifiers. Appendix A
lists all of the reserved words in Java. In the code in this book, they are colored red.


Now that we’ve seen how to write identifiers, let’s look at some of the things that Java
allows us to name.


Built-in Types in Java


A computer program operates on data. In Java, each piece of data must be of a specific data
type. The data type determines how the data is represented in the computer and the kinds


Letter
_
$

_...
$

Letter
Digit

Field A named place in mem-
ory that holds a data object
Reserved word A word that
has special meaning in Java; it
cannot be used as a
programmer-defined identifier
Free download pdf