1 / 18

Comp 401 Memory Representation of Primitive Values and Objects

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.

Télécharger la présentation

Comp 401 Memory Representation of Primitive Values and Objects

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. Comp 401Memory Representation of Primitive Values and Objects Instructor: PrasunDewan

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

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

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

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

  6. Instance-Specific Structure double angle double APolarPoint radius location width AnAnotherLine int height int newAnAnotherLine(newAPolarPoint (14.01, 0.78), 20, 20)

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

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

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

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

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

  12. Extra Slides

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

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

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

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

  17. Assignment of Object Variables p2 p1 ACartesianPoint@8

More Related