1 csce3193: programming paradigms nilanjan banerjee programming paradigms university of arkansas...

14
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR [email protected] http://www.csce.uark.edu/~nilanb/ 3193/S10/

Upload: arline-carpenter

Post on 28-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

1

CSCE3193: Programming Paradigms

Nilanjan Banerjee

Programming Paradigms

University of ArkansasFayetteville, AR

[email protected]://www.csce.uark.edu/~nilanb/3193/S10/

Page 2: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

2

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)

0 1 0 1 0 1 0 1

Page 3: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

3

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

Page 4: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

4

Numbers and base systems

• The following diagrams show how the number forty-two appears in both octal and hexadecimal notation:

2 x =

21

5 x =

408

5 2

42

10 x =

101

02 x =

3216

2 A

42

octal hexadecimal

Page 5: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

5

Addressing memory

• 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

• In Java there is no way of finding out what memory address an object is stored

000000040008000C001000140018001C002000240028002C

FFD0FFD4FFD8FFDCFFE0FFE4FFE8FFECFFF0FFF4FFF8FFFC

Page 6: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

6

Allocation of memory to variables

• 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)

• 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

staticdata

heap

stack

0000

FFFF

Page 7: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

7

Use of stack heap diagram

• Whenever you run a program, it is beneficial to draw a visualization in the form of a stack heap diagram.

• 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.

staticdata

heap

stack

0000

FFFF

Page 8: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

Object References

• Internally, Java identifies an object by its address in memory. That address is called a reference.

stack

1000r1 FFFC

• The local variable r1 is allocated in the current stack frame and is assigned the value 1000, which identifies the object.

• The next slide traces the execution of the TestRational program that I will show you right now.

Rational r1 = new Rational(1, 2);

heap

2den

1num1008

1004

1000

it allocates heap space for the new Rational object. For this example, imagine that the object is created at address 1000.

• As an example, when Java evaluates the declaration

Page 9: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

A Complete Heap-Stack Trace

skip simulation

abc

sum

FFFC

FFF8

FFF4

FFF0

FFEC

1000100C

1018

1030

TestRationalstack

This object is a temporary valueused only during the calculation.

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

public void run() { Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); Rational c = new Rational(1, 6); Rational sum = a.add(b).add(c); println(a + " + " + b + " + " + c + " = " + sum);}

1den

1num

6den

5num

6den

1num

3den

1num

2den

1num

public Rational add(Rational r) {

return new Rational( this.num * r.den + r.num * this.den , this.den * r.den );

} 6

51 3 1 2

public Rational add(Rational r) {

return new Rational( this.num * r.den + r.num * this.den , this.den * r.den );

} 36

36

heap

1038

1034

1030

102C

1028

1024

1020

101C

1018

1014

1010

100C

1008

1004

1000

1000

100C

thisr

FFE8

FFE4

FFE0

1000

100C

1024

1018

This stack frame is createdfor the run method.

This stack frame is createdfor the add method.

All objects are createdin the heap.

TestRational

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

public void run() { Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); Rational c = new Rational(1, 6); Rational sum = a.add(b).add(c); println(a + " + " + b + " + " + c + " = " + sum);}

1den

1num

6den

5num

6den

1num

3den

1num

2den

1num

heap stack

abc

sum

FFFC

FFF8

FFF4

FFF0

FFEC

1000100C

1018

1030

1038

1034

1030

102C

1028

1024

1020

101C

1018

1014

1010

100C

1008

1004

1000

Page 10: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

The Pointer Model

1den

1num

6den

5num

6den

1num

3den

1num

2den

1num

heap stack

abc

sum

FFFC

FFF8

FFF4

FFF0

FFEC

1000100C

1018

1030

1038

1034

1030

102C

1028

1024

1020

101C

1018

1014

1010

100C

1008

1004

1000

• 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.

1den

1num

6den

5num

6den

1num

3den

1num

2den

1num

abcsum

heap stack

Page 11: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

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.

1den

1num

6den

5num

6den

1num

3den

1num

2den

1num

abcsum

heap stack

This object was used to hold a temporaryresult and is no longer accessible.

Page 12: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

Primitive Types vs. Objects

• At first glance, Java’s 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 object—usually by calling setters—have 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.

Page 13: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

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…

Page 14: 1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu nilanb/3193/S10

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