Please visit http pingo upb de643250 https xkcd
Please visit http: //pingo. upb. de/643250 https: //xkcd. com/138/
Five-Minute Review 1. What are local/instance/class variables? What about constants? 2. What is an array? 3. How do we locally declare an array of 5 integers? 4. How is the above array stored? 5. What are multi-dimensional arrays? 2
Five-Minute Review 1. What do &, |, ~, ^ mean for integers? 2. What are typical uses for & and |? 3. What is a generic class? 4. What are Array. Lists, when should they be used? 5. What is a pixel, how is it encoded? 3
Programming – Lecture 8 Objects and Memory (Chapter 7) • Memory structure • Allocation of memory to variables – Heap, Stack • Recursion (for this only: Chapter 14) • Linking objects together 4
Memory Bits, bytes, words Variable storage Static data: class var's Heap: instance var's Stack: local var's static data 0000 0001 0004 0002 0008 0000 000 C 0003 0004 0010 0005 0014 0006 0018 0007 001 C heap. . . 0020 0008 0024 0009 0028 000 A 000 B 002 C FFD 0 FFF 4 FFF 5 FFD 4 FFD 8 FFF 6 FFDC FFF 7 FFF 8 FFE 0 FFF 9 FFE 4 FFE 8 FFFA FFEC FFFB FFFC FFF 0 FFFD FFF 4 stack FFFF 8 FFFE FFFC FFFF 8
Initialization Automatic initialization to default value for • Class var’s • Instance var’s • Array elements Not for local var’s! 9
Object References • Reference of object: address where object is stored • Object var's store object references • Object var's reference objects, or point to objects • In general, var's containing memory addresses are also referred to as pointers Rational r 1 = new Rational(1, 2); heap stack 1000 num den 1 1004 2 1008 r 1 1000 Note: this assumes r 1 to be local var FFFC
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 stack Test. Rational 1000 num den 1 1004 2 1008 100 C num den 1 1010 3 1014 1018 num den 1 101 C 6 1020 1/2 + 1/3 + 1/6 = 1 All objects are created in the heap. This object is a temporary value used only during calculation. r the FFE 0 100 C 1018 this 1000 1024 FFE 8 1024 num den 5 1028 6 102 C 1030 num den 1 1034 1 1038 FFE 4 This stack frame is created for the add method. sum c b a 1030 FFEC 1018 FFF 0 100 C 1000 FFF 4 FFF 8 FFFC This stack frame is created for the run method. skip 13 simulation
Address Model vs. Pointer Model heap stack 1000 num den 1 1004 2 1008 100 C num den 1 1010 3 1014 1018 num den 1 101 C 6 1020 1024 num den 5 1028 6 102 C 1030 num den 1 1034 1 1038 sum c b a 1030 FFEC 1018 FFF 0 100 C 1000 FFF 4 FFF 8 FFFC num den 1 num den 5 num den 1 2 3 6 sum c b a 6 1 14
Garbage Collection heap This object was used to hold a temporary result and is no longer accessible num den 1 num den 5 num den 1 stack 2 3 6 6 sum c b a 1 Mark-and-sweep collection, in-use flags 16
Exercise: Stack-Heap Diagrams public class Point { public Point(int cx, int cy) { this. cx = cx; this. cy = cy; } } public class Line { public Line(Point start, Point end) { this. start = start; this. end = end; } . . . other methods appear here. . . private int cx; private int cy; private Point start; private Point end; } public void run() { Point p 1 = new Point(0, 0); Point p 2 = new Point(200, 200); Line line = new Line(p 1, p 2); } 17
Address Model Pointer Model heap stack 1000 cx cy 0 1004 0 1008 cx cy 0 cx cy 200 0 100 C cx cy 200 1010 200 1014 1018 start end 1000 101 C 100 C 1020 line p 2 p 1 1018 FFF 0 100 C 1000 FFF 4 FFF 8 FFFC 200 line p 2 p 1 start end 18
Recursion • Recursion: method calls itself • Direct recursion: a() calls a() • Indirect recursion: a() calls b(), which calls a() • Allowing recursion is motivation to use a stack for method calls; stack permits multiple stack frames for the same method
Carl Burch, Programming with Java (Online book) http: //www. toves. org/books/java/ch 18 -recurex/ import java. awt. *; import acm. program. *; import acm. graphics. *; public class Tree extends Graphics. Program { public void run() { set. Size(500, 350; ( draw. Tree(250, 350, 100, 90); } public void draw. Tree(. . . ) {. . . } } 32
public void draw. Tree(double x 0, double y 0, double len, double angle) { if (len > 2) { double x 1 = x 0 + len * GMath. cos. Degrees(angle); double y 1 = y 0 – len * GMath. sin. Degrees(angle); add(new GLine(x 0, y 0, x 1, y 1)); draw. Tree(x 1, y 1, len * 0. 75, angle + 30); draw. Tree(x 1, y 1, len * 0. 66, angle - 50); } } 33
Linking Objects Together Minas Tirith Amon Dîn Eilenach Nardol Erelas Min. Rimmon Calenhad Halifirien Rohan Linked list: data link null 36
public class Signal. Tower { /* Private instance variables */ private String tower. Name; private Signal. Tower next. Tower; Minas Tirith Min-Rimmon Amon Dîn Calenhad Eilenach Halifirien Nardol Rohan /* Constructs a new signal tower */ public Signal. Tower(String tower. Name, Signal. Tower next. Tower) { this. tower. Name = tower. Name; this. next. Tower = next. Tower; } /* Signals this tower and passes the * message along to the next one. */ public void signal() { light. Current. Tower(); if (next. Tower != null) { next. Tower. signal(); } } null Erelas } /* Marks this tower as lit */ public void light. Current. Tower() {. . . code to draw a fire on this tower. . . } 37
Summary I • Computer memory is a sequence of addressable bytes • char / int / double require 2 / 4 / 8 bytes • Memory is organized in three regions: 1. Static data: program code, static variables 2. Heap: objects, instance variables (new) 3. Stack: local variables, references to objects (pointers) • Stacks are dynamic last-in, first-out (LIFO) data structures (push + pop) 38
Summary II • • • Using a stack for method data allows an arbitrary number of method instances, which facilitates recursion Stack frame of a method call includes this pointer Garbage collection reclaims unused memory in heap (mark-and-sweep) In method calls, primitive type are passed by value, objects are passed by reference; thus objects are shared between caller and callee Automatic boxing/unboxing transforms between primitive types and their corresponding wrapper classes Objects can contain references to other objects – use this e. g. for linked lists 39
- Slides: 19