COMP 401 MEMORY REPRESENTATION OF PRIMITIVE VALUES AND OBJECTS Instructor: Prasun Dewan STORING PRIMITIVE VALUES AND VARIABLES int i = 5; address variables Memory Block Memory blocks are of the same size memory 32

Download Report

Transcript COMP 401 MEMORY REPRESENTATION OF PRIMITIVE VALUES AND OBJECTS Instructor: Prasun Dewan STORING PRIMITIVE VALUES AND VARIABLES int i = 5; address variables Memory Block Memory blocks are of the same size memory 32

COMP 401
MEMORY REPRESENTATION OF
PRIMITIVE VALUES AND OBJECTS
Instructor: Prasun Dewan
STORING PRIMITIVE VALUES AND VARIABLES
int i = 5;
address
variables
16
Memory
Block
Memory
blocks are of
the same
size
memory
5
32 bits
52
int i
32 bits
2
STORING PRIMITIVE VALUES AND VARIABLES
double d = 5.5;
address
variables
8
Memory
Block
Memory
blocks are of
the same
size
double e = d;
memory
5.5
64 bits
64 bits
48
double d
80
double e
5.5
3
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.

4
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.

5
INSTANCE-SPECIFIC STRUCTURE
angle
double
double
APolarPoint
AnAnotherLine
width
radius
int
int
new AnAnotherLine(new APolarPoint (14.01, 0.78), 20, 20)
6
INSTANCE-SPECIFIC STRUCTURE
int
x
ACartesianPoint
AnAnotherLine
width
int
y
int
int
new AnAnotherLine(new ACartesianPoint (10, 10), 20, 20)
Structures of instances of same class can be different!
7
STORING OBJECT VALUES AND VARIABLES
public class ACartesianPoint
implements Point {
int x
int y;
…
}
address
variables
memory
8
ACartesianPoint@8
50
100
Instance variables
stored in memory
Point p1 = new
ACartesianPoint(50,100);
52
Memory blocks
are of different
size!
Point p1
8
Address of object
copied to block
Object variables are pointers to memory blocks
8
ASSIGNMENT OF OBJECT VARIABLES
Point p1 = new
address
variables
memory
8
ACartesianPoint@8
50
100
52
Point p1
8
56
Point p2
ACartesianPoint(50,100);
Point p2
p2
=
p1;
p1
ACartesianPoint@8
9
ASSIGNMENT OF OBJECT VARIABLES
Point p1 = new
address
variables
memory
8
ACartesianPoint@8
100
50
100
52
Point p1
8
56
Point p2
8
ACartesianPoint(50,100);
Point p2
=
p1;
p1.setX(100);
p2.getX();
p2
 100
p1
ACartesianPoint@8
10
ASSIGNMENT OF OBJECT VARIABLES
Point p1 = new
address
variables
memory
8
ACartesianPoint@8
100
50
100
52
Point p1
76
8
56
Point p2
8
76
ACartesianPoint@76
150
50
75
ACartesianPoint(50,100);
Point p2
=
p1;
p1.setX(100);
p2.getX();
 100
Point p1 = new
ACartesianPoint(150,75);
p2.getX();
p2
 100
p1
ACartesianPoint@8
ACartesianPoint@76
11
12
EXTRA SLIDES
13
STORING PRIMITIVE VALUES AND VARIABLES
int i = 5;
address
variables
16
Memory
Block
Memory
blocks are of
the same
size
memory
5
32 bits
52
int i
5
32 bits
14
STORING PRIMITIVE VALUES AND VARIABLES
double i = 5.5;
address
variables
8
Memory
Block
Memory
blocks are of
the same
size
double e = d;
memory
5.5
64 bits
64 bits
48
double d
5.5
80
double e
5.5
15
STORING OBJECT VALUES AND VARIABLES
public class ACartesianPoint
implements Point {
int x
int y;
…
}
address
variables
memory
8
ACartesianPoint@8
50
100
Instance variables
stored in memory
Point p1 = new
ACartesianPoint(50,100);
52
Memory blocks
are of different
size!
Point p1
8
Address of object
copied to block
Object variables are pointers to memory blocks
16
ASSIGNMENT OF OBJECT VARIABLES
Point p1 = new
address
variables
memory
8
ACartesianPoint@8
50
100
52
Point p1
8
56
Point p2
8
ACartesianPoint(50,100);
Point p2
=
p1;
17
ASSIGNMENT OF OBJECT VARIABLES
p2
p1
ACartesianPoint@8
18