Documente Academic
Documente Profesional
Documente Cultură
Nilanjan Banerjee
University of Arkansas
Fayetteville, AR
nilanb@uark.edu
http://www.csce.uark.edu/~nilanb/3193/S10/
Programming Paradigms
Memory model
Smallest unit of memory is called a bit (short form of binary
digit). A bit can be one of two states: 0 or a 1
Memory is usually organized as a collection of bits
8 bits together is called a byte
Numbers are organized in units larger than a byes
Word: refers to a collection of bytes (usually 4 bytes)
octal
hexadecimal
2
2
5
x
x
1 = 2
8 = 40
10
02
42
1 = 10
x 16
= 32
x
42
Addressing memory
0000
0004
0008
000C
0010
0014
0018
001C
0020
0024
0028
002C
FFD0
FFD4
FFD8
FFDC
FFE0
FFE4
FFE8
FFEC
FFF0
FFF4
FFF8
FFFC
static
data
heap
stack
FFFF
static
data
heap
stack
FFFF
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
heap
num
den
1004
1004
1008
1008
100C
100C
num
den
1010
1010
1014
1014
1018
1018
num
den
stack
1000
1000
101C
101C
1020
1020
1000
1024
1028
1028
102C
102C
1030
1030
num
den
1034
1034
1038
1038
FFE4
FFE8
1024
1024
num
den
TestRational
sum
c
b
a
1030
FFEC
1018
FFF0
100C
1000
FFF4
FFF8
FFFC
skip simulation
stack
1000
num
den
1004
1008
num
den
1010
1014
num
den
101C
1020
num
den
1028
102C
num
den
1034
1038
heap
num
den
num
den
num
den
num
den
num
den
stack
100C
3
1018
6
1024
1030
sum
c
b
a
1030
FFEC
1018
FFF0
100C
1000
FFF4
FFF8
FFFC
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
Parameter Passing
Can occur by value or reference
Apart from primitive types, everything else (objects) are
passed by reference
However, you always pass a copy of the reference
Impossible to write a swap method on Objects in Java
Lets see an example to understand this
Concept of binding
Language time binding, compile time, load time, link time, or
runtime
count = count + 5
Count is bound at compile time
The possible values that count could have is language time
bounded
Meaning of + is compile time bounded
The representation of 5 is compile time bounded
The value of count after the operation is runtime bounded