180 likes | 294 Views
Comp 401 Memory Representation of Primitive Values and Objects. Instructor: Prasun Dewan. Storing Primitive Values and Variables. int i = 5;. address. variables. memory. 16. 5. 5. Memory Block. 32 bits. Memory blocks are of the same size. 52. int i. 32 bits.
E N D
Comp 401Memory Representation of Primitive Values and Objects Instructor: PrasunDewan
Storing Primitive Values and Variables inti = 5; address variables memory 16 5 5 Memory Block 32 bits Memory blocks are of the same size 52 inti 32 bits
Storing Primitive Values and Variables double d = 5.5; address variables memory 5.5 5.5 8 Memory Block 64 bits 64 bits Memory blocks are of the same size 5.5 48 double d double e = d; 80 double e
Storing Primitive Values • 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.
Storing Objects • 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.
Instance-Specific Structure double angle double APolarPoint radius location width AnAnotherLine int height int newAnAnotherLine(newAPolarPoint (14.01, 0.78), 20, 20)
Instance-Specific Structure int x int ACartesianPoint y location width AnAnotherLine int height int newAnAnotherLine(newACartesianPoint (10, 10), 20, 20) Structures of instances of same class can be different!
Storing Object Values and Variables publicclassACartesianPoint implements Point { int x inty; … } address variables memory 8 ACartesianPoint@8 50 100 Instance variables stored in memory Point p1 = new ACartesianPoint(50,100); 52 Point p1 8 Memory blocks are of different size! Address of object copied to block Object variables are pointers to memory blocks
Assignment of Object Variables Point p1 = new ACartesianPoint(50,100); address variables memory 8 ACartesianPoint@8 50 Point p2 = p1; 100 52 Point p1 8 8 56 Point p2 p2 p1 ACartesianPoint@8
Assignment of Object Variables Point p1 = new ACartesianPoint(50,100); address variables memory 8 ACartesianPoint@8 100 50 Point p2 = p1; 100 p1.setX(100); 52 Point p1 8 8 p2.getX(); 100 56 Point p2 8 p2 p1 ACartesianPoint@8
Assignment of Object Variables Point p1 = new ACartesianPoint(50,100); address variables memory 8 ACartesianPoint@8 100 50 Point p2 = p1; 100 p1.setX(100); 52 Point p1 76 8 p2.getX(); 100 Point p1 = new ACartesianPoint(150,75); 56 Point p2 8 p2.getX(); 100 76 ACartesianPoint@76 150 50 75 p2 p1 ACartesianPoint@8 ACartesianPoint@76
Storing Primitive Values and Variables inti = 5; address variables memory 16 5 Memory Block 32 bits Memory blocks are of the same size 52 inti 5 32 bits
Storing Primitive Values and Variables double i = 5.5; address variables memory 5.5 5.5 8 Memory Block 64 bits 64 bits Memory blocks are of the same size 5.5 48 double d 5.5 double e = d; 80 double e
Storing Object Values and Variables publicclassACartesianPoint implements Point { int x inty; … } address variables memory 8 ACartesianPoint@8 50 100 Instance variables stored in memory Point p1 = new ACartesianPoint(50,100); 52 Point p1 8 Memory blocks are of different size! Address of object copied to block Object variables are pointers to memory blocks
Assignment of Object Variables Point p1 = new ACartesianPoint(50,100); address variables memory 8 ACartesianPoint@8 50 Point p2 = p1; 100 52 Point p1 8 56 Point p2 8
Assignment of Object Variables p2 p1 ACartesianPoint@8