Pointers Prasun Dewan Comp 114
Pointers Primitive values vs. objects Wrapper Classes Memory Representation of Primitive Values Vs Objects Pointers Equal Vs == Sharing of Objects Garbage Collection Shallow vs. Deep copy
(new Integer(5)).toString() Wrapper Classes “Joe Doe”.toString() Object Vector vector = new Vector() Number Integer vector.addElement(“Joe Doe”) (new Integer(5)).toString() new Integer(5) 5 String AStringHistory vector.addElement(new Integer(5)) Double vector.elementAt(3).intValue() vector.elementAt(3) AStringDatabase wrapper class wrapper class 5.toString() char boolean double int vector.addElement(5)
Other Wrapper Classes Double Boolean Character Float, Short, Long public Double(double value) public double doubleValue() Boolean public Boolean(boolean value) public boolean booleanValue() Character public Character(char value) public char charValue() Float, Short, Long
Storing Primitive Values/Variables 8 5.5 int i = 5; same, double size memory address 5 16 same size 48 double d 5.5 double d = 5.5; 52 int i 5 double e = d; 80 double e 5.5
Storing Objects Values/Variables 5.5 Double@8 8 Integer I = new Integer(5) different sizes Double D = new Double(5.5) 5 16 Integer@16 48 Double D 8 variables same size memory address 60 Integer I 16
Structured Objects public class APoint implements Point { int x, y; public APoint (int initX, int initY) { x = initX; y = initY; } public int getX() {return x}; public void setX(int newVal) {x = newVal;} public int getY() {return y}; public void setY(int newVal) {y = newVal;}
Structured Objects 5.5 Double@8 8 5 16 Integer@16 48 Double D 60 public class APoint implements Point { // instance vars int x, y; //methods … } 5.5 Double@8 8 5 16 Integer@16 48 Double D 60 Integer I 16 Point p = new APoint(50, 100) 50 100 80 APoint@80 96 Point p 80
Superclass Constructor Inheritance public class ABoundedPoint extends APoint { APoint upperLeftCorner, lowerRightCorner; public ABoundedPoint (int initX, int initY, Point initUpperLeftCorner, Point initLowerRightCorner) { super(initX, initY); upperLeftCorner = initUpperLeftCorner; lowerRightCorner = initLowerRightCorner; } … Superclass Constructor
Inheritance 50 APoint@8 8 100 16 APoint@16 48 ABoundedPoint@48 75 8 16 public class APoint implements Point { int x, y; … } 50 APoint@8 8 100 16 APoint@16 public class ABoundedPoint extends APoint { Point upperLeftCorner ; Point lowerRightCorner; … } 48 ABoundedPoint@48 75 8 16 new ABoundedPoint(75, 75, new APoint(50,50), new APoint(100,100) )
Assignment of Object Variables Point p1 = new APoint(50, 50); 8 50 APoint@8 Point p2 = p1; 50 p1.setX(100); 16 Point p1 8 P1 P2 APoint@8 48 Point p2 8
Assignment of Object Variables Point p1 = new APoint(50, 50); 8 100 APoint@8 Point p2 = p1; 50 p1.setX(100); p2.getX() 100 16 8 Point p1 p1 = new APoint(200,200); P1 P2 APoint@8 48 Point p2 8
Assignment of Object Variables Point p1 = new APoint(50, 50); 8 100 APoint@8 Point p2 = p1; 50 p1.setX(100); p2.getX() 100 16 64 Point p1 p1 = new APoint(200,200); p2.getX() 100 p2 = p1; 48 8 Point p2 P1 P2 APoint@64 APoint@8 64 200 APoint@64 200
Assignment of Object Variables Point p1 = new APoint(50, 50); 8 100 APoint@8 Point p2 = p1; 50 p1.setX(100); p2.getX() 100 16 64 Point p1 p1 = new APoint(200,200); p2.getX() 100 p2 = p1; p2.getX() 200 48 64 Point p2 P1 P2 APoint@64 APoint@8 64 200 APoint@64 200 Garbage collected
== for Objects Point p1 = new APoint(200, 200); 8 200 APoint@8 p2 == p2 false Same physical object? 16 64 Point p1 48 8 Point p2 P1 P2 APoint@64 APoint@8 64 200 APoint@64 200
== for Objects Point p1 = new APoint(200, 200); 8 200 APoint@8 Point p2 = p1; 200 p2 == p2 true 16 8 Point p1 P1 P2 APoint@8 48 8 Point p2
== vs. Equal for Strings String s1 = “Joe Doe”; 8 Joe Doe String@8 false 16 64 String s1 s1.equals(s2) true 48 8 String s2 S1 S2 String@64 String@8 64 Joe Doe String@64
== vs. equals() for Objects Point p1 = new APoint(200, 200); 8 200 APoint@8 Point p2 = new APoint(200, 200) 200 p2 == p2 false 16 64 Point p1 p1.equals(p2) true 48 8 Point p2 64 200 APoint@64 public boolean equals(Point otherPoint) { return x == otherPoint.getX() && y == otherPoint.getY(); } 200
Copying Objects p1 = new APoint(200, 200); p2 = p1; p1.setX (100); What if we want copy rather than reference. The properties can be changed independently Backup
Copier does the work p1 = new APoint(200, 200); p2 = new APoint (p1.getX(), p1.getY()); p1.setX (100); What if we want copy rather than reference. The properties can be changed independently Backup
Copied object does the work p1 = new APoint(200, 200); p2 = p1.copy(); p1.setX (100); Class APoint defines: public Point copy() { return new APoint(x, y); } All copiers reuse the copy code
Bounded Point Copy? public ABoundedPoint copy() { return new ABoundedPoint (x, y, upperLeftCorner, lowerRightCorner); };
Shallow Copy
Deep Copy
Bounded Point Deep Copy public ABoundedPoint copy() { return new ABoundedPoint (x, y, upperLeftCorner.copy(), lowerRightCorner.copy()); };
Shallow vs. Deep Copy Shallow copy: Deep copy Copies the instance but not its components Deep copy Copies the instance and deep copies the components