07 ObjectsAndMemory
07 ObjectsAndMemory
CHAPTER 7
ERIC S. ROBERTS
7.1
7.2
7.3
7.4
An Introduction
to Computer Science
Binary Notation
Bytes and words can be used to represent integers of different
sizes by interpreting the bits as a number in binary notation.
Binary notation is similar to decimal notation but uses a
different base. Decimal numbers use 10 as their base, which
means that each digit counts for ten times as much as the digit
to its right. Binary notation uses base 2, which means that
each position counts for twice as much, as follows:
0
The next
Thedigit
next
The
gives
digit
rightmost
gives digit
And so on . . .
the number
the number
ofis4s.
theofunits
2s. place.
0
1
0
1
0
1
0
0
x
x
x
x
x
x
x
x
1
2
4
8
16
32
64
128
=
=
=
=
=
=
=
=
0
2
0
8
0
32
0
0
42
hexadecimal
2
2
5
x
x
1 = 2
8 = 40
42
10
02
x
x
1 = 10
16 = 32
42
100012
1
170
1778
AD16
7 7
127
A
173D
1 x 1 = 1
7 x 1 = 7
13 x 1 = 13
0 x 2 = 0
7 x 8 = 56
10 x 16 = 160
As part of a code to identify
the
file
type,
every
Java
class file
0 x 4 = 0
1 x 64 = 64
173
begins with the following
sixteen
bits:
0 x 8 = 0
127
x
1 16 = 1
1 1 0 0 1 0 1 017 1 1 1 1 1 1 1 0
F
CAFE16
.
.
.
.
.
.
0000
0001
0004
0002
0008
0003
000C
0004
0010
0005
0014
0006
0018
0007
001C
0008
0020
0009
0024
000A
0028
000B
002C
FFD0
FFF4
FFF5
FFD4
FFF6
FFD8
FFF7
FFDC
FFF8
FFE0
FFE4
FFF9
FFFA
FFE8
FFFB
FFEC
FFFC
FFF0
FFFD
FFF4
FFFE
FFF8
FFFC
FFFF
0000
static
data
heap
stack
FFFF
Heap-Stack Diagrams
It is easier to understand how Java works if you have a good
mental model of its use of memory. The text illustrates this
model using heap-stack diagrams, which show the heap on
the left and the stack on the right, separated by a dotted line.
Whenever your program creates a new object, you need to
add a block of memory to the heap side of the diagram. That
block must be large enough to store the instance variables for
the object, along with some extra space, called overhead, that
is required for any object. Overhead space is indicated in
heap-stack diagrams as a crosshatched box.
Whenever your program calls a method, you need to create a
new stack frame by adding a block of memory to the stack
side. For method calls, you need to add enough space to store
the local variables for the method, again with some overhead
information that tracks what the program is doing. When a
method returns,
Java reclaims the memory
in its frame.
Object References
Internally, Java identifies an object by its address in memory.
That address is called a reference.
As an example, when Java evaluates the declaration
Rational r1 = new Rational(1, 2);
it allocates heap space for the new Rational object. For this
example, imagine that the object is created at address 1000.
The local variable r1 is allocated in the current stack frame and is assigned the value
1000, which identifies the object.
heap
stack
1000
num
den
1004
1008
r1
1000
FFFC
stack
TestRational
1000
1000
num
den
1004
1004
1008
1008
num
den
1010
1010
1014
1014
num
den
101C
101C
1020
1020
num
den
1028
1028
102C
102C
num
den
100C
100C
1018
1018
1000
1024
FFE8
1024
1024
1030
1030
1
1034
1034
1038
1038
FFE4
sum
c
b
a
1030
FFEC
1018
FFF0
100C
1000
FFF4
FFF8
FFFC
skip simulation
heap
stack
heap
stack
1000
num
den
1004
1008
num
den
1010
1014
num
den
101C
1020
num
den
1028
102C
num
den
1034
1038
100C
1018
1024
1030
sum
c
b
a
1030
FFEC
1018
FFF0
100C
1000
FFF4
FFF8
FFFC
num
den
num
den
num
den
num
den
num
den
sum
c
b
a
The address model makes it clear that references have numeric values.
The pointer model emphasizes the relationship between the reference and the object and makes the diagram easier to
follow.
heap
stack
heap
stack
1000
num
den
1004
1008
num
den
1010
1014
num
den
101C
1020
num
den
1028
102C
num
den
1034
1038
100C
1018
1024
1030
sum
c
b
a
1030
FFEC
1018
FFF0
100C
1000
FFF4
FFF8
FFFC
num
den
num
den
num
den
num
den
num
den
sum
c
b
a
Garbage Collection
One fact that the pointer model makes clear in this diagram is that there are no longer any
references to the Rational value 5/6. That value has now become garbage.
From time to time, Java runs through the heap and reclaims
any garbage. This process is called garbage collection.
heap
num
den
num
den
num
den
num
den
num
den
stack
sum
c
b
a
Pointer Model
stack
heap
heap
stack
1000
cx
cy
1004
1008
cx
cy
200
1010
200
1014
start
finish
1000
101C
100C
1020
cx
cy
cx
cy
200
100C
line
p2
p1
1018
1018
FFF0
100C
1000
FFF8
200
line
p2
p1
FFF4
FFFC
start
finish
Wrapper Classes
The designers of Java chose to separate the primitive types
from the standard class hierarchy mostly for efficiency.
Primitive values take less space and allow Java to use more of
the capabilities provided by the hardware.
Even so, there are times in which the fact that primitive types are not
objects gets in the way. There are many tools in the Java libraries
several of which you will encounter later in the bookthat work only
with objects.
To get around this problem, Java includes a wrapper class to correspond to
each of the primitive types:
boolean
byte
char
double
Boolean
Byte
Character
Double
float
int
long
short
Float
Integer
Long
Short
stack
1000
1004
five
1000
FFFC
data
link
data
link
data
link
null
Java marks the end of linked list using the constant null,
which signifies a reference that does not actually point to an
actual object. The value null has several other uses, as you
will discover in the chapters that follow.
Minas Tirith
Amon Dn
Eilenach
Nardol
Erelas
Min-Rimmon
Calenhad
Halifirien
Rohan
Min-Rimmon
Amon Dn
Calenhad
Eilenach
Halifirien
Nardol
Rohan
null
Erelas
TheEnd