C OMP 401 M EMORY R EPRESENTATION OF P RIMITIVE V ALUES AND O BJECTS Instructor: Prasun Dewan
2 S TORING P RIMITIVE V ALUES AND V ARIABLES variablesmemory 516 address int i = 5; int i52 5 Memory Block Memory blocks are of the same size 32 bits
3 S TORING P RIMITIVE V ALUES AND V ARIABLES variablesmemory address double d = 5.5; double d Memory Block Memory blocks are of the same size 64 bits double e = d; 80double e 5.5
4 S TORING P RIMITIVE V ALUES Values and variables of same type take same amount of fixed storage. The storage consists of one or more consecutive memory words that together form a memory block. Values and variables of different types may take different storage.
5 S TORING O BJECTS Can we assign all variables and objects of the same object type the same sized memory block? No: A variable can be assigned instances of different classes. Different instances of the same class can take different amount of space.
6 I NSTANCE -S PECIFIC S TRUCTURE new AnAnotherLine( new APolarPoint (14.01, 0.78), 20, 20) AnAnotherLine APolarPoint int location height width double angle radius double
7 I NSTANCE -S PECIFIC S TRUCTURE AnAnotherLine ACartesianPoint int location height width int x y new AnAnotherLine( new ACartesianPoint (10, 10), 20, 20) Structures of instances of same class can be different!
8 S TORING O BJECT V ALUES AND V ARIABLES variablesmemoryaddress Point p1 = new ACartesianPoint(50,100); 100 public class ACartesianPoint implements Point { int x int y; … } Instance variables stored in memory Point p1528 Address of object copied to block Memory blocks are of different size! Object variables are pointers to memory blocks
9 A SSIGNMENT OF O BJECT V ARIABLES variablesmemoryaddress Point p1 = new ACartesianPoint(50,100); 100 Point p2 = p1; Point p1528 Point p256 8 p2p1
10 A SSIGNMENT OF O BJECT V ARIABLES variablesmemoryaddress Point p1 = new ACartesianPoint(50,100); 100 Point p2 = p1; Point p1528 Point p256 8 p2p1 p1.setX(100); 100 p2.getX(); 100 8
11 A SSIGNMENT OF O BJECT V ARIABLES variablesmemoryaddress Point p1 = new ACartesianPoint(50,100); 100 Point p2 = p1; Point p1528 Point p256 p2p1 p1.setX(100); 100 p2.getX(); Point p1 = new ACartesianPoint(150,75); p2.getX(); 100
12
13 E XTRA S LIDES
14 S TORING P RIMITIVE V ALUES AND V ARIABLES variablesmemory 516 address int i = 5; int i525 Memory Block Memory blocks are of the same size 32 bits
15 S TORING P RIMITIVE V ALUES AND V ARIABLES variablesmemory address double i = 5.5; double d Memory Block Memory blocks are of the same size 64 bits double e = d; 80double e 5.5
16 S TORING O BJECT V ALUES AND V ARIABLES variablesmemoryaddress Point p1 = new ACartesianPoint(50,100); 100 public class ACartesianPoint implements Point { int x int y; … } Instance variables stored in memory Point p1528 Address of object copied to block Memory blocks are of different size! Object variables are pointers to memory blocks
17 A SSIGNMENT OF O BJECT V ARIABLES variablesmemoryaddress Point p1 = new ACartesianPoint(50,100); 100 Point p2 = p1; Point p1528 Point p2568
18 A SSIGNMENT OF O BJECT V ARIABLES p2p1