1 / 24

Chapter 7—Objects and Memory

Java. The Art and Science of. An Introduction. to Computer Science. ERIC S. ROBERTS. C H A P T E R 7. Objects and Memory. Chapter 7—Objects and Memory. Yea, from the table of my memory I’ll wipe away all trivial fond records. —William Shakespeare, Hamlet, c. 1600 .

oren
Download Presentation

Chapter 7—Objects and Memory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS C H A P T E R 7 Objects and Memory Chapter 7—Objects and Memory Yea, from the table of my memory I’ll wipe away all trivial fond records. —William Shakespeare, Hamlet, c. 1600  7.1 The structure of memory 7.2 The allocation of memory to variables 7.3 Primitive types vs. objects 7.4 Linking objects together

  2. The hardware structure of a computer combines individual bits into larger units. In most modern architectures, the smallest unit on which the hardware operates is a sequence of eight consecutive bits called a byte. The following diagram shows a byte containing a combination of 0s and 1s: 0 0 1 0 1 0 1 0 The Structure of Memory • The fundamental unit of memory inside a computer is called a bit, which is a contraction of the words binary digit. A bit can be in either of two states, usually denoted as 0 and 1. • Numbers are stored in still larger units that consist of multiple bytes. The unit that represents the most common integer size on a particular hardware is called a word. Because machines have different architectures, the number of bytes in a word may vary from machine to machine.

  3. Binary notation is similar to decimal notation but uses a different base. Decimal numbers use 10 as their base, which means that each digit counts for ten times as much as the digit to its right. Binary notation uses base 2, which means that each position counts for twice as much, as follows: 0 0 1 0 1 0 1 0 1 0 x = 0 2 1 x = 2 4 0 x = 0 8 1 x = 8 16 0 x = 0 32 1 x = 32 64 0 x = 0 128 0 x = 0 42 Binary Notation • Bytes and words can be used to represent integers of different sizes by interpreting the bits as a number in binary notation. And so on . . . The next digit gives the number of 4s. The next digit gives the number of 2s. The rightmost digit is the units place.

  4. The number 42 is what you get if you count how many stars are in the pattern at the right. The number is the same whether you write it in English as forty-two, in decimal as 42, or in binary as 00101010. Numbers and Bases • The calculation at the end of the preceding slide makes it clear that the binary representation 00101010 is equivalent to the number 42. When it is important to distinguish the base, the text uses a small subscript, like this: 001010102 = 4210 • Although it is useful to be able to convert a number from one base to another, it is important to remember that the number remains the same. What changes is how you write it down. • Numbers do not have bases; representations do.

  5. The following diagrams show how the number forty-two appears in both octal and hexadecimal notation: octal hexadecimal 5 2 2 A 1 1 2 x = 2 10 x = 10 8 16 5 x = 40 02 x = 32 42 42 Octal and Hexadecimal Notation • Because binary notation tends to get rather long, computer scientists often prefer octal (base 8) or hexadecimal (base 16) notation instead. Octal notation uses eight digits: 0 to 7. Hexadecimal notation uses sixteen digits: 0 to 9, followed by the letters A through F to indicate the values 10 to 15. • The advantage of using either octal or hexadecimal notation is that doing so makes it easy to translate the number back to individual bits because you can convert each digit separately.

  6. 1 0 0 0 1 1 1 x = 1 2 0 x = 0 • As part of a code to identify the file type, every Java class file begins with the following sixteen bits: 4 0 x = 0 8 0 x = 0 16 1 x = 1 1 1 0 0 1 0 1 0 1 1 1 1 1 1 1 0 How would you express that number in hexadecimal notation? 1 7 7 A D 1 1 0 0 1 0 1 0 1 1 1 1 1 1 1 0 1 1 0 0 1 0 1 0 1 1 1 1 1 1 1 0 1 1 7 x = 7 13 x = 13 8 16 7 x = 56 10 x = 160 A F E 64 1 x = 64 173 CAFE16 127 17 Exercises: Number Bases • What is the decimal value for each of the following numbers? 100012 1778 AD16 17 127 173

  7. 0000 0000 0004 0001 0008 0002 000C 0003 0010 0004 0014 0005 0018 0006 001C 0007 0020 0008 0024 0009 0028 000A 002C 000B FFD0 FFF4 FFD4 FFF5 FFD8 FFF6 FFDC FFF7 FFE0 FFF8 FFE4 FFF9 FFE8 FFFA FFEC FFFB FFF0 FFFC FFF4 FFFD FFF8 FFFE FFFC FFFF Memory and Addresses • Every byte inside the primary memory of a machine is identified by a numeric address. The addresses begin at 0 and extend up to the number of bytes in the machine, as shown in the diagram on the right. • In these slides as well as in the diagrams in the text, memory addresses appear as four-digit hexadecimal numbers, which makes addresses easy to recognize. . . . . . . • In Java, it is impossible to determine the address of an object. Memory addresses used in the examples are therefore chosen completely arbitrarily. • Memory diagrams that show individual bytes are not as useful as those that are organized into words. The revised diagram on the right now includes four bytes in each of the memory cells, which means that the address numbers increase by four each time.

  8. One region of memory is reserved for variables that are never created or destroyed as the program runs, such as named constants and other class variables. This information is called static data. 0000 static data heap • Whenever you create a new object, Java allocates space from a pool of memory called the heap. • Each time you call a method, Java allocates a new block of memory called a stack frame to hold its local variables. These stack frames come from a region of memory called the stack. stack • In classical architectures, the stack and heap grow toward each other to maximize the available space. FFFF The Allocation of Memory to Variables • When you declare a variable in a program, Java allocates space for that variable from one of several memory regions.

  9. Heap-Stack Diagrams • It is easier to understand how Java works if you have a good mental model of its use of memory. The text illustrates this model using heap-stack diagrams, which show the heap on the left and the stack on the right, separated by a dotted line. • Whenever your program creates a new object, you need to add a block of memory to the heap side of the diagram. That block must be large enough to store the instance variables for the object, along with some extra space, called overhead, that is required for any object. Overhead space is indicated in heap-stack diagrams as a crosshatched box. • Whenever your program calls a method, you need to create a new stack frame by adding a block of memory to the stack side. For method calls, you need to add enough space to store the local variables for the method, again with some overhead information that tracks what the program is doing. When a method returns, Java reclaims the memory in its frame.

  10. 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 1004 1 r1 den FFFC 1008 1000 2 Object References • Internally, Java identifies an object by its address in memory. That address is called a reference. • The next slide traces the execution of the TestRational program from Chapter 6 using heap-stack model.

  11. 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 ); } heap stack TestRational 1000 1000 num num 1004 1004 1 1 1/2 + 1/3 + 1/6 = 1 All objects are created in the heap. den den 1008 1008 2 2 100C 100C num num 1010 1010 1 1 den den 1014 1014 3 3 Thisobjectisatemporaryvalue used only during the calculation. 1018 1018 This stack frame is created for the add method. r num num FFE0 101C 101C 100C 1 1 1018 this den den FFE4 1020 1020 1000 6 6 1024 FFE8 1024 1024 sum sum num num FFEC FFEC 1028 1028 5 5 This stack frame is created for the run method. 1030 c c den den FFF0 FFF0 102C 102C 6 6 1018 b b FFF4 FFF4 1030 1030 100C a a num num FFF8 FFF8 1034 1034 1 1 1000 den den FFFC FFFC 1038 1038 1 1 A Complete Heap-Stack Trace 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); } 5 36 2 1 3 1 36 6 heap stack TestRational 1/2 + 1/3 + 1/6 = 1 100C 1000 1030 1018 100C 1000 skip simulation

  12. 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 num 1004 1 1 den den 1008 2 2 100C num num 1010 1 1 den den 1014 3 3 1018 num num 101C 1 1 den den 1020 6 6 1024 sum sum num num FFEC 1028 5 5 c c den den FFF0 102C 6 6 b b FFF4 1030 a a num num FFF8 1034 1 1 den den FFFC 1038 1 1 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. heap stack 1030 1018 100C 1000

  13. heap stack heap stack 1000 num num 1004 1 1 den den 1008 2 2 100C num num 1010 1 1 den den 1014 3 3 1018 num num 101C 1 1 den den 1020 6 6 1024 sum 1030 sum num num FFEC 1028 5 5 c 1018 c den den FFF0 102C 6 6 b 100C b FFF4 1030 a 1000 a num num FFF8 1034 1 1 den den FFFC 1038 1 1 Addresses vs. Pointers • The two heap-stack diagram formats—the address model and the pointer model—describe exactly the same memory state. The models, however, emphasize different things: • The address model makes it clear that references have numeric values. • The pointer model emphasizes the relationship between the reference and the object and makes the diagram easier to follow.

  14. num 1 den 2 num 1 den 3 This object was used to hold a temporary result and is no longer accessible. num 1 den 6 num 5 den 6 num 1 den 1 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 stack sum c b a

  15. Exercise: Stack-Heap Diagrams Suppose that the classes Point and Line are defined as follows: public class Point { public Point(int x, int y) { cx = x; cy = y; } . . . other methods appear here . . . private int cx; private int cy; } public class Line { public Line(Point p1, Point p2) { start = p1; finish = p2; } . . . other methods appear here . . . private Point start; private Point finish; } 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); }

  16. 1000 cx cx 1004 0 0 cy cy 1008 0 0 100C stack heap stack cx cx heap 1010 200 200 cy cy 1014 200 200 1018 start start 101C 1000 finish finish 1020 100C line line FFF0 1018 p2 p2 FFF4 100C p1 p1 FFF8 1000 FFFC Solution: Stack-Heap Diagrams Address Model Pointer Model

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

  18. To get around this problem, Java includes a wrapper class to correspond to each of the primitive types: boolean Boolean float Float byte Byte int Integer char Character long Long double Double short Short Wrapper Classes • The designers of Java chose to separate the primitive types from the standard class hierarchy mostly for efficiency. Primitive values take less space and allow Java to use more of the capabilities provided by the hardware. • Even so, there are times in which the fact that primitive types are not objects gets in the way. There are many tools in the Java libraries—several of which you will encounter later in the book—that work only with objects.

  19. For each of the wrapper classes, Java defines a method to retrieve the primitive value, as illustrated below: int underlyingValue = five.intValue(); Using Wrapper Classes • You can create an instance of a wrapper class by calling its constructor with the primitive value. For example, the line Integer five = new Integer(5); creates a new Integer object containing the value 5: heap stack 1000 five 1004 FFFC 5 1000 • To value stored in the variable five is a real object, and you can use it in any contexts that require objects.

  20. Similarly, if you then write int six = five + 1; Java will automatically call intValue before the addition. Boxing and Unboxing • As of Java Standard Edition 5.0, Java automatically converts values back and forth between a primitive type and the corresponding wrapper class. For example, if you write Integer five = 5; Java will automatically call the Integer constructor. • These operations are called boxing and unboxing. • Although boxing and unboxing can be quite convenient, this feature can generate confusion and should be used with care.

  21. One common example (which you will encounter again in Chapter 13) is called a linked list, in which each object in a sequence contains a reference to the one that follows it: data data data data link link link link null Linking Objects Together • Although most examples of this technique are beyond the scope of a first course, references are particularly important in computer science because they make it possible to represent the relationship among objects by linking them together in various ways. • Java marks the end of linked list using the constant null, which signifies a reference that does not actually point to an actual object. The value null has several other uses, as you will discover in the chapters that follow.

  22. The Beacons of Gondor For answer Gandalf cried aloud to his horse. “On, Shadowfax! We must hasten. Time is short. See! The beacons of Gondor are alight, calling for aid. War is kindled. See, there is the fire on Amon Dîn, and flame on Eilenach; and there they go speeding west: Nardol, Erelas, Min-Rimmon, Calenhad, and the Halifirien on the borders of Rohan.” —J. R. R. Tolkien, The Return of the King, 1955 In a scene that was brilliantly captured in Peter Jackson’s film adaptation of The Return of the King, Rohan is alerted to the danger to Gondor by a succession of signal fires moving from mountain top to mountain top. This scene is a perfect illustration of the idea of message passing in a linked list. Minas Tirith Amon Dîn Eilenach Nardol Erelas Min-Rimmon Calenhad Halifirien Rohan

  23. Message Passing in Linked Structures To represent this message-passing image, you might use a definition such as the one shown on the right. public class SignalTower{ /* Constructs a new signal tower */ public SignalTower(String name, SignalTower link){ towerName = name; nextTower = link; } /* * 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 . . . } /* Private instance variables */ private String towerName; private SignalTower nextTower; } You can then initialize a chain of SignalTower objects, like this: Minas Tirith Min-Rimmon Amon Dîn Calenhad Eilenach Halifirien Nardol Rohan null Erelas Calling signal on the first tower sends a message down the chain.

  24. The End

More Related