Java Reference
In-Depth Information
example, if n is an int variable, then n can contain a value of type int , such as 42 . If v
is a variable of a class type, then v does not directly contain an object of its class. Instead,
v names an object by containing the memory address of where the object is located in
memory. In this section, we discuss how a variable of a class type names objects, and we
also discuss the related topic of how method parameters of a class type behave in Java.
Variables and Memory
A computer has two forms of memory called secondary memory and main memory . The
secondary memory is used to hold files for more or less permanent storage. The main
memory is used by the computer when it is running a program. Values stored in a
program's variables are kept in this main memory. It will help our understanding of class
type variables to learn a few details about how program variables are represented in main
memory. For now, assume that each variable in a program is of some primitive type,
such as int , double , or char . Once you understand how variables of a primitive type
are stored in memory, it will be easier to describe how variables of a class type behave.
Main memory consists of a long list of numbered locations called bytes , each
containing eight bits; that is, eight 0/1 digits. The number that identifies a byte is
called its address . A data item, such as a number or a letter, can be stored in one
of these bytes, and the address of the byte is then used to find the data item when
it is needed.
Most data types have values that require more than one byte of storage. When a
data type requires more than one byte of storage, several adjacent bytes are used to
hold the data item. In this case, the entire chunk of memory that holds the data item
is still called a memory location . The address of the first of the bytes that make up
this memory location is used as the address for this larger memory location. Thus,
as a practical matter, you can think of the computer's main memory as a long list of
memory locations of varying sizes . The size of each of these locations is expressed in
bytes, and the address of the first byte is used as the address (name) of that memory
location. Display 5.10 shows a picture of a hypothetical computer's main memory.
Each primitive type variable in a program is assigned one of these memory locations,
and the value of the variable is stored in this memory location.
secondary
and main
memory
byte
address
memory
location
variables of
a primitive
type
Bytes and Addresses
Main memory is divided into numbered locations called bytes . The number associated with
a byte is called its address . A group of consecutive bytes is used as the location for the
value of a variable. The address of the first byte in the group is used as the address of this
larger memory location.
 
Search WWH ::




Custom Search