Download presentation
Presentation is loading. Please wait.
Published byΔιογένης Σκλαβούνος Modified over 5 years ago
1
Five-Minute Review What are local/instance/class variables? What about constants? What is an array? How do we locally declare an array of 5 integers? How is the above array stored? What are multi-dimensional arrays? Local variables: local to a method. Instance variables: associated with object. Class variables: associated with class. Constants: Variables that cannot change anymore after initialization, use final modifier, typically class variables Array: ordered collection of homogeneous elements. Int[] intArr = new int[5]; intArr is stored on stack, stores pointer to array stored on heap, latter consists of 5 integers + overhead Arrays with multiple indices, arrays of arrays
2
Five-Minute Review What do &, |, ~, ^ mean for integers?
What are typical uses for & and |? What is a generic class? What are ArrayLists, when should they be used? What is a pixel, how is it encoded? Bit-wise AND, OR, NOT, XOR & used for masking, | used for assembling values Class parameterized over types. Extensible arrays, should be used when size is not known in advance Pixel = element in image bit map; encoded as 32 bit int, transparency (alpha, 0 = fully transparent) + red + green + blue
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 Randomness in games
4
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 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 000A 000B FFF4 FFF5 FFF6 FFF7 FFF8 FFF9 FFFA FFFB FFFC FFFD FFFE FFFF Bits, bytes, words Variable storage Static data: class var's Heap: instance var's Stack: local var's static data 0000 heap Static data, constants Heap: Dynamic variables, ”new” Stack: local variables, parameters Stack in general is Last In, First Out (LIFO) data structure, corresponding to call chains: last method called is the first method returned from. . . stack FFFF
5
Initialization Automatic initialization to default value for
Class var’s Instance var’s Array elements Not for local var’s!
6
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 r1 = new Rational(1, 2); Note: this assumes r1 to be local var heap stack 1000 r1 FFFC 1000 num 1 1004 den 2 1008
7
A Complete Heap-Stack Trace
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 num 6 5 3 2 heap stack a b c sum FFFC FFF8 FFF4 FFF0 FFEC 1000 100C 1018 1030 1038 1034 102C 1028 1024 1020 101C 1014 1010 1008 1004 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); } public Rational add(Rational r) { return new Rational( this.num * r.den + r.num * this.den , this.den * r.den ); } public Rational add(Rational r) { return new Rational( this.num * r.den + r.num * this.den , this.den * r.den ); } 36 5 1 3 1 2 6 36 heap stack TestRational 2 den 1 num All objects are created in the heap. 1008 1004 1000 1/2 + 1/3 + 1/6 = 1 3 den 1 num 1014 1010 100C This object is a temporary value used only during the calculation. 6 den 1 num 1020 101C 1018 1000 100C this r FFE8 FFE4 FFE0 This stack frame is created for the add method. 1018 100C 1000 1024 6 den 5 num 102C 1028 1024 a b c sum FFFC FFF8 FFF4 FFF0 FFEC This stack frame is created for the run method. 1030 1018 1 den num 1038 1034 1030 100C 1000 skip simulation
8
Address Model vs. Pointer Model
1 den num 6 5 3 2 a b c sum heap stack heap stack 2 den 1 num 1008 1004 1000 3 den 1 num 1014 1010 100C 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. 6 den 1 num 1020 101C 1018 6 den 5 num 102C 1028 1024 a b c sum FFFC FFF8 FFF4 FFF0 FFEC 1030 1018 1 den num 1038 1034 1030 100C 1000
9
Garbage Collection Mark-and-sweep collection, in-use flags heap stack
2 den 1 num 3 den 1 num This object was used to hold a temporary result and is no longer accessible 6 den 1 num 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. Mark-and-sweep collector: Go through every variable reference on the stack and every variable reference in static storage and mark the corresponding object as being “in use” by setting a flag that lives in the object’s overhead space. In addition, whenever you set the “in use” flag for an object, determine whether that object contains references to other objects and mark those objects as being “in use” as well. Go through every object in the heap and delete any objects for which the “in use” flag has not been set, returning the storage used for that object to the heap for subsequent reallocation. It must be safe to delete these objects, because there is no way for the program to gain access to them, given that no references to those objects still exist in the program variables. 6 den 5 num sum c 1 den num b a Mark-and-sweep collection, in-use flags
10
Exercise: Stack-Heap Diagrams
public class Point { public Point(int cx, int cy) { this.cx = cx; this.cy = cy; } . . . other methods appear here . . . private int cx; private int cy; public class Line { public Line(Point start, Point end) { this.start = start; this.end = end; } . . . other methods appear here . . . private Point start; private Point end; Suppose that the classes Point and Line are defined as follows: Draw a heap-stack diagram showing the state of memory just before the following run method returns. public void run() { Point p1 = new Point(0, 0); Point p2 = new Point(200, 200); Line line = new Line(p1, p2); }
11
Address Model Pointer Model heap stack heap stack end start cy cx p1
1000 start 200 cy cx heap stack p1 p2 line FFFC FFF8 FFF4 FFF0 1018 1020 101C 1014 1010 1008 1004 end start 200 cy cx p1 p2 line heap stack Solution
12
Please visit http://pingo.upb.de/643250
13
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 Recursion
14
Carl Burch, Programming with Java (Online book) http://www. toves
Carl Burch, Programming with Java (Online book) import java.awt.*; import acm.program.*; import acm.graphics.*; public class Tree extends GraphicsProgram { public void run() { setSize(500, 350); drawTree(250, 350, 100, 90); } public void drawTree(...) { ... }
15
public void drawTree(double x0, double y0, double len, double angle) { double x1 = x0 + len * GMath.cosDegrees(angle); double y1 = y0 – len * GMath.sinDegrees(angle); add(new GLine(x0, y0, x1, y1)); if (len > 2) { drawTree(x1, y1, len * 0.75, angle + 30); drawTree(x1, y1, len * 0.66, angle - 50); }
16
Linking Objects Together
Minas Tirith Amon Dîn Eilenach Nardol Erelas Min-Rimmon Calenhad Halifirien Rohan Linked list: link data null
17
public class SignalTower { /* Private instance variables */
private String towerName; private SignalTower nextTower; /* Constructs a new signal tower */ public SignalTower(String towerName, SignalTower nextTower) { this.towerName = towerName; this.nextTower = nextTower; } /* Signals this tower and passes the * message along to the next one. */ public void signal() { lightCurrentTower(); if (nextTower != null) { nextTower.signal(); /* Marks this tower as lit */ public void lightCurrentTower() { . . . code to draw a fire on this tower . . . Minas Tirith Amon Dîn Eilenach Nardol Erelas Min-Rimmon Calenhad Halifirien Rohan null Message Passing in Linked Structures To represent this message-passing image, you might use a definition such as the one shown on the right. You can then initialize a chain of SignalTower objects, like this: Calling signal on the first tower sends a message down the chain.
18
Summary I Computer memory is a sequence of addressable bytes
char / int / double require 2 / 4 / 8 bytes Memory is organized in three regions: Static data: program code, static variables Heap: objects, instance variables (new) Stack: local variables Stacks are dynamic last-in, first-out (LIFO) data structures (push + pop)
19
Summary II Using a stack for method data allows an arbitrary number of method instances, which facilitates recursion 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.