Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^54) | Java Syntax and Semantics, Classes, and Objects
Java provides operations for joining strings, comparing strings, copying portions of
strings, changing the case of letters in strings, converting numbers to strings, and convert-
ing strings to numbers. We will look at some of these operations later in this chapter and cover
the remaining operations in subsequent chapters.


Defining Terms: Declarations


How do we tell the computer what an identifier represents? We use a declaration, a statement
that associates a name (an identifier) with a description of an element in a Java
program (just as a dictionary definition associates a name with a description of the
thing being named). In a declaration, we name both the identifier and what it rep-
resents.
When we declare an identifier, the compiler picks a location in memory to be
associated with it. We don’t have to know the actual address of the memory lo-
cation because the computer automatically keeps track of it for us.
To see how this process works, suppose that when we mail a letter, we have
to put only a person’s name on it and the post office will look up the address. Of
course, everybody in the world would need a different name with such a system; otherwise,
the post office wouldn’t be able to figure out whose address was whose. The same is true in
Java. Each identifier can represent just one thing (except under special circumstances, which
we talk about later). Every identifier you use in your code must be different from all others.

Field Declarations


Classes are made up of methods and fields. We’ve seen that methods are the operations
that are associated with the data in an object. Fields are the components of a class that rep-
resent the data. Data in a class can be of any type, including the primitive types or objects.
It’s important to grasp the significance of being able to have objects within objects, as it al-
lows us to gradually build up objects of great complexity.
We use identifiers to refer to fields. In Java you must declare every identifier before it is
used. The compiler can then verify that the use of the identifier is consistent with its decla-
ration. If you declare an identifier to be a field that can hold a charvalue and later try to
store a number into the field, for example, the compiler will detect this inconsistency and
issue an error message.
A field can be either a constant or a variable. In other words, a field identifier can be the
name of a memory location whose contents are not allowed to change or it can be the name
of a memory location whose contents can change. There are different forms of declaration
statements for variables and constants in Java. First we look at variables, then consider con-
stants, and finally look at fields in general. Declarations for methods and classes are covered
later in the chapter.

Variables Data is stored in memory. While an application is executing, different values may


be stored in the same memory location at different times. This kind of memory location is

Declaration A statement that
associates an identifier with a
field, a method, a class, or a pack-
age so that the programmer can
refer to that item by name
Free download pdf