Sunteți pe pagina 1din 14

CSCE3193: Programming Paradigms

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)

Numbers and base systems


There are different ways of representing numbers
Binary is base 2
Octal is base 8
Hexadecimal is base 16
There are just representation of numbers
Lets take a few examples to try to understand them

Numbers and base systems

The following diagrams show how the number forty-two


appears in both octal and hexadecimal notation:

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

Every byte in memory has an address


That address is represented as a
hexadecimal
On the side memory is represented as a
4-digit hexadecimal number
The number of digits depends on the
complete size of your memory

0018
001C
0020
0024
0028
002C

FFD0
FFD4
FFD8
FFDC
FFE0
FFE4
FFE8
FFEC
FFF0
FFF4
FFF8
FFFC

In Java there is no way of finding out what


memory address an object is stored

Allocation of memory to variables


0000

When you declare a variable Java allocates


memory from one of the many memory
regions
One region of the memory is reserved for
variables that are neither created nor
destroyed while your program runs---static
variables, constants (final)

static
data

heap

When a dynamic variable is created using


new, Java allocates memory from something
called heap that grows from top to bottom
Each time a method is called, Java allocates
something called a stack frame for its
variables, the stack frame is allocated from a
memory region called the stack
6

stack

FFFF

Use of stack heap diagram


0000

Whenever you run a program, it is beneficial


to draw a visualization in the form of a stack
heap diagram.

static
data

Whenever a program creates a new object a


block of memory from the heap must be
allocated to store the instance variables along
with some additional metadata called
overhead
Whenever your program calls a method, you
need to create a new stack frame by adding
memory to the stack. Allocate memory
enough for the local variables, and some
overhead to keep track of what the program
is doing.

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

The next slide traces the execution of the TestRational


program that I will show you right now.

A Complete Heap-Stack Trace


public void run() {
Rational a = new Rational(1, 2);
public Rational add(Rational r) {
36
5
Rational b = new Rational(1, 3);
2
1
3
1
Rational c = new Rational(1, 6);
return new Rational( this.num * r.den + r.num * this.den ,
Rational sum = a.add(b).add(c);
this.den * r.den );
println(a + " + " + b + " + " + c + " = " + sum);
}}
36
6

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

1/2 + 1/3 + 1/6 = 1

All objects are created


in the heap.
This object is a temporary value
FFE0
100C
used only during
r the
1018
calculation.
this

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

This stack frame is created


for the add method.
This stack frame is created
for the run method.

FFF8
FFFC

skip simulation

The Pointer Model


The heap-stack diagram at the lower left shows the state of
memory at the end of the run method from TestRational.
The diagram at the lower right shows exactly the same state
using arrows instead of numeric addresses. This style of
diagram is said to use the pointer model.
heap

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

This object was used to hold a temporary


result and is no longer accessible.

num
den

num
den

num
den

num
den

num
den

stack

sum
c
b
a

Primitive Types vs. Objects


At first glance, Javas rules for passing objects as arguments
seem to differ from the rules Java uses with arguments that
are primitive types.
When you pass an argument of a primitive type to a method,
Java copies the value of the argument into the parameter
variable. As a result, changes to the parameter variable have
no effect on the argument.
When you pass an object as an argument, there seems to be
some form of sharing going on. Although changing the
parameter variable itself has no effect, any changes that you
make to the instance variables inside an objectusually by
calling settershave a permanent effect on the object.
Stack-heap diagrams make the reason for this seeming
asymmetry clear. When you pass an object to a method, Java
copies the reference but not the object itself.

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

S-ar putea să vă placă și