Download presentation
Presentation is loading. Please wait.
Published byArline Carpenter Modified over 9 years ago
1
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR nilanb@uark.edu http://www.csce.uark.edu/~nilanb/3193/S10/
2
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) 01010101
3
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
4
4 Numbers and base systems The following diagrams show how the number forty-two appears in both octal and hexadecimal notation: 2 x = 2 1 5 x = 40 8 52 42 10 x = 10 1 02 x = 32 16 2A 42 octalhexadecimal
5
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 0000 0004 0008 000C 0010 0014 0018 001C 0020 0024 0028 002C FFD0 FFD4 FFD8 FFDC FFE0 FFE4 FFE8 FFEC FFF0 FFF4 FFF8 FFFC
6
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 static data heap stack 0000 FFFF
7
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. static data heap stack 0000 FFFF
8
Object References Internally, Java identifies an object by its address in memory. That address is called a reference. stack 1000 r1 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 2 den 1 num 1008 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
9
A Complete Heap-Stack Trace skip simulation a b c sum FFFC FFF8 FFF4 FFF0 FFEC 1000 100C 1018 1030 TestRational stack This object is a temporary value used 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); } 1 den 1 num 6 den 5 num 6 den 1 num 3 den 1 num 2 den 1 num public Rational add(Rational r) { return new Rational( this.num * r.den + r.num * this.den, this.den * r.den ); } 6 5 1 3 1 2 public Rational add(Rational r) { return new Rational( this.num * r.den + r.num * this.den, this.den * r.den ); } 36 heap 1038 1034 1030 102C 1028 1024 1020 101C 1018 1014 1010 100C 1008 1004 1000 100C this r FFE8 FFE4 FFE0 1000 100C 1024 1018 This stack frame is created for the run method. This stack frame is created for the add method. All objects are created in 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); } 1 den 1 num 6 den 5 num 6 den 1 num 3 den 1 num 2 den 1 num heapstack a b c sum FFFC FFF8 FFF4 FFF0 FFEC 1000 100C 1018 1030 1038 1034 1030 102C 1028 1024 1020 101C 1018 1014 1010 100C 1008 1004 1000
10
The Pointer Model 1 den 1 num 6 den 5 num 6 den 1 num 3 den 1 num 2 den 1 num heapstack a b c sum FFFC FFF8 FFF4 FFF0 FFEC 1000 100C 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. 1 den 1 num 6 den 5 num 6 den 1 num 3 den 1 num 2 den 1 num a b c sum heapstack
11
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. 1 den 1 num 6 den 5 num 6 den 1 num 3 den 1 num 2 den 1 num a b c sum heapstack This object was used to hold a temporary result and is no longer accessible.
12
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.
13
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…
14
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.