lec 23 memory maps primitive variables and reference (object) variables
Post on 22-Feb-2016
35 Views
Preview:
DESCRIPTION
TRANSCRIPT
Lec 23 Memory MapsPrimitive variables and
Reference (object) variables
Reminder
• Primitive variables – of type int, double, char, boolean– the variable contains the data
• Object variables– of type String, Point, Pad, Dairy, int [] (i.e. array)– refer to an object (or contain null)– actual data (the object) is elsewhere in memory
A primitive/object example int i = 2; int j = i; i = 3; System.out.println(i); System.out.println(j);
Point p = new Point(2,2); Point q = p; p.setLocation(3,3); System.out.println(p); System.out.println(q);
Expected Output??: 3 2 java.awt.Point[x=3,y=3] java.awt.Point[x=2,y=2] java.awt.Point[x=3,y=3]
A primitive/object example int i = 2; int j = i; i = 3; System.out.println(i); System.out.println(j);
Point p = new Point(2,2); Point q = p; p.setLocation(3,3); System.out.println(p); System.out.println(q);
2i
A primitive/object example int i = 2; int j = i; i = 3; System.out.println(i); System.out.println(j);
Point p = new Point(2,2); Point q = p; p.setLocation(3,3); System.out.println(p); System.out.println(q);
2i
2j
A primitive/object example int i = 2; int j = i; i = 3; System.out.println(i); System.out.println(j);
Point p = new Point(2,2); Point q = p; p.setLocation(3,3); System.out.println(p); System.out.println(q);
3i
2j
A primitive/object example int i = 2; int j = i; i = 3; System.out.println(i); print 3 System.out.println(j);
Point p = new Point(2,2); Point q = p; p.setLocation(3,3); System.out.println(p); System.out.println(q);
3i
2j
A primitive/object example int i = 2; int j = i; i = 3; System.out.println(i); System.out.println(j); print 2
Point p = new Point(2,2); Point q = p; p.setLocation(3,3); System.out.println(p); System.out.println(q);
3i
2j
A primitive/object example int i = 2; int j = i; i = 3; System.out.println(i); System.out.println(j);
Point p = new Point(2,2); Point q = p; p.setLocation(3,3); System.out.println(p); System.out.println(q);
3i
2j
p
point x y
2
2
A primitive/object example int i = 2; int j = i; i = 3; System.out.println(i); System.out.println(j);
Point p = new Point(2,2); Point q = p; p.setLocation(3,3); System.out.println(p); System.out.println(q);
3i
2j
p
q
point x y
2
2
A primitive/object example int i = 2; int j = i; i = 3; System.out.println(i); System.out.println(j);
Point p = new Point(2,2); Point q = p; p.setLocation(3,3); System.out.println(p); System.out.println(q);
3i
2j
p
q
point x y
3
3
A primitive/object example int i = 2; int j = i; i = 3; System.out.println(i); System.out.println(j);
Point p = new Point(2,2); Point q = p; p.setLocation(3,3); System.out.println(p); print 3,3 System.out.println(q);
3i
2j
p
q
point x y
3
3
A primitive/object example int i = 2; int j = i; i = 3; System.out.println(i); System.out.println(j);
Point p = new Point(2,2); Point q = p; p.setLocation(3,3); System.out.println(p); System.out.println(q); print 3,3
3i
2j
p
q
point x y
3
3
Why is this?
• This exemplifies a key difference between primitive variables and reference (i.e. object) variables: For primitives types, we have primitive variables, which actually store a value – Assignment statements for primitive variables actually
copy the value over • For class types, we have reference variables, which
only store a reference to an object – Assignment statements for reference variables only copy a
pointer over
Memory diagrams • Memory diagrams are a very useful tool for
understanding what's going on inside memory • We will use the following conventions:
– Represent a variable with an oval containing its type (optional), name, and contents (which goes inside a box within the oval)
• For primitive variables, put the value right in the box • For reference (class type) variables, draw an arrow from that box to
the actual object which is elsewhere on the diagram – Represent objects as boxes; the type goes on top and is
underlined, any variables within the object are listed inside the box (the ovals can be omitted in this case)
Memory Map -- Assignment
• When an assignment statement happens: – If the receiving variable is a primitive type, just
copy the value into its box – If the receiving variable is a class type, make its
arrow point to whatever object it is getting assigned to
– In other words, always copy what's in the box - it will be either a pointer or a primitive value!
More practice on primitive versus reference variables
• Challenge: How could we make it so that we actually make a true copy of the Point in the last example? – One way is to make a new Point and then copy the desired x and y
coordinates into it – Another way is to make a new Point using the desired x and y
coordinates directly – This example makes a true copy of a Point and does some pointer
manipulation. • Here is a sequence of memory diagrams for that code
• When an object no longer has references to it, you can no longer get to it and it eventually gets erased by the garbage collector; this frees up the memory it was using
Point p = new Point(2,2); Point q = new Point(); q.setLocation(p);
p.setLocation(3,3); System.out.println(p); //prints a point at (3,3) System.out.println(q); //prints a point at (2,2) p = q; //Makes p point to q's object; this is NOT a true copy System.out.println(p); //prints a point at (2,2) System.out.println(q); //prints a point at (2,2)
How to make a true copy of a Point
p
point x y
2
2
How to make a true copy of a Point
Point p = new Point(2,2); Point q = new Point(); q.setLocation(p);
p.setLocation(3,3); System.out.println(p); //prints a point at (3,3) System.out.println(q); //prints a point at (2,2) p = q; //Makes p point to q's object; this is NOT a true copy System.out.println(p); //prints a point at (2,2) System.out.println(q); //prints a point at (2,2)
p
q
point x y
2
2
point x y
0
0
How to make a true copy of a Point
Point p = new Point(2,2); Point q = new Point(); q.setLocation(p);
p.setLocation(3,3); System.out.println(p); //prints a point at (3,3) System.out.println(q); //prints a point at (2,2) p = q; //Makes p point to q's object; this is NOT a true copy System.out.println(p); //prints a point at (2,2) System.out.println(q); //prints a point at (2,2)
p
q
point x y
2
2
point x y
2
2
How to make a true copy of a Point
Point p = new Point(2,2); Point q = new Point(); q.setLocation(p);
p.setLocation(3,3); System.out.println(p); //prints a point at (3,3) System.out.println(q); //prints a point at (2,2) p = q; //Makes p point to q's object; this is NOT a true copy System.out.println(p); //prints a point at (2,2) System.out.println(q); //prints a point at (2,2)
p
q
point x y
3
3
point x y
2
2
How to make a true copy of a Point
Point p = new Point(2,2); Point q = new Point(); q.setLocation(p);
p.setLocation(3,3); System.out.println(p); //prints a point at (3,3) System.out.println(q); //prints a point at (2,2) p = q; //Makes p point to q's object; this is NOT a true copy System.out.println(p); //prints a point at (2,2) System.out.println(q); //prints a point at (2,2)
p
q
point x y
3
3
point x y
2
2
How to make a true copy of a Point
Point p = new Point(2,2); Point q = new Point(); q.setLocation(p);
p.setLocation(3,3); System.out.println(p); //prints a point at (3,3) System.out.println(q); //prints a point at (2,2) p = q; //Makes p point to q's object; this is NOT a true copy System.out.println(p); //prints a point at (2,2) System.out.println(q); //prints a point at (2,2)
p
q
point x y
3
3
point x y
2
2
How to make a true copy of a Point
Point p = new Point(2,2); Point q = new Point(); q.setLocation(p);
p.setLocation(3,3); System.out.println(p); //prints a point at (3,3) System.out.println(q); //prints a point at (2,2) p = q; //Makes p point to q's object; this is NOT a true copy System.out.println(p); //prints a point at (2,2) System.out.println(q); //prints a point at (2,2)
p
q
point x y
3
3
point x y
2
2
How to make a true copy of a Point
Point p = new Point(2,2); Point q = new Point(); q.setLocation(p);
p.setLocation(3,3); System.out.println(p); //prints a point at (3,3) System.out.println(q); //prints a point at (2,2) p = q; //Makes p point to q's object; this is NOT a true copy System.out.println(p); //prints a point at (2,2) System.out.println(q); //prints a point at (2,2)
p
q
point x y
3
3
point x y
2
2
How to make a true copy of a Point
Point p = new Point(2,2); Point q = new Point(); q.setLocation(p);
p.setLocation(3,3); System.out.println(p); //prints a point at (3,3) System.out.println(q); //prints a point at (2,2) p = q; //Makes p point to q's object; this is NOT a true copy System.out.println(p); //prints a point at (2,2) System.out.println(q); //prints a point at (2,2)
p
q point
x y
2
2
Some technical jargon for methods:
• Mutator methods: methods that change the state of the object – For the Particle class: passTime and reset are mutator methods
• Accessor methods: methods that return info about the state of the object – For the Particle class: getXPosition, getYPosition, getXVelocity, and getYVelocity
are accessor methods
• Predicate methods: methods that return a boolean (true/false value) – For the Particle class: isFalling is a predicate method
• Invoking a method with dot syntax is called calling a method or sending a message to that object
• When calling a method, the values given between the parentheses are called arguments
• If you get something back as a result of calling a method, it is called a return value
Edit-complile-test cycle • You may have noticed that when programming, we usually:
– 1) Edit the source code a little bit – 2) Compile to make sure we have the syntax right (automatic in
Eclipse) – 3) Test our program to see if it behaves the way we expected so
far – 4) Go back to 1) until we are done writing the program
• This process is known as the edit-compile-test cycle • It is often better to make small changes and check your
work so you can spot and correct errors quickly and before they become confounding
top related