Pointers Prasun Dewan Comp 114. Pointers Primitive values vs. objects Wrapper Classes Memory...

26
Pointers Prasun Dewan Comp 114

Transcript of Pointers Prasun Dewan Comp 114. Pointers Primitive values vs. objects Wrapper Classes Memory...

  • Slide 1

Pointers Prasun Dewan Comp 114 Slide 2 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 Slide 3 vector.elementAt(3) 5new Integer(5) Wrapper Classes AStringDatabase AStringHistoryString Object intbooleanchardouble wrapper class Joe Doe.toString() 5.toString() Vector vector = new Vector() vector.addElement(Joe Doe) vector.addElement(5) vector.addElement(new Integer(5)) (new Integer(5)).toString() vector.elementAt(3).intValue() Number Integer Double wrapper class Slide 4 Other Wrapper Classes Double 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 Slide 5 Storing Primitive Values/Variables 5 5.5 48 double d 52 int i 80 double e 85.5 5 16 5 int i = 5; memory address double d = 5.5; same size same, double size double e = d; Slide 6 Storing Objects Values/Variables 16 8 5.5 Double@8 860 Integer I 5 16 Integer@16 48 Double D Integer I = new Integer(5) Double D = new Double(5.5) memory address different sizes variables same size Slide 7 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;} } Slide 8 Structured Objects 16 80 5.5 Double@8 860 Integer I 5 16 Integer@16 96 Point p 50 100 80 APoint@80 48 Double D public class APoint implements Point { // instance vars int x, y; //methods } Point p = new APoint(50, 100) Slide 9 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 Slide 10 Inheritance public class APoint implements Point { int x, y; } new ABoundedPoint(75, 75, new APoint(50,50), new APoint(100,100) ) 8 16 50 APoint@8 8 48 ABounded Point@48 100 16 APoint@16 100 75 public class ABoundedPoint extends APoint { Point upperLeftCorner ; Point lowerRightCorner; } Slide 11 Assignment of Object Variables Point p1 = new APoint(50, 50); Point p2 = p1; 50 APoint@8 8 8 16 Point p1 8 48 Point p2 P1P2 APoint@8 p1.setX(100); Slide 12 Assignment of Object Variables Point p1 = new APoint(50, 50); Point p2 = p1; 100 50 APoint@8 8 16 8 Point p1 8 48 Point p2 p1.setX(100); p2.getX() 100 P1P2 APoint@8 p1 = new APoint(200,200); Slide 13 Assignment of Object Variables 100 50 200 APoint@8 64 200 16 48 8 Point p1 APoint@64 8Point p2 64 Point p2 = p1; p1 = new APoint(200,200); p2.getX() 100 p1.setX(100); p2.getX() 100 Point p1 = new APoint(50, 50); P1P2 APoint@64APoint@8 p2 = p1; Slide 14 Assignment of Object Variables 100 50 200 APoint@8 64 200 16 48 8 Point p1 APoint@64 64Point p2 64 Point p2 = p1; p1 = new APoint(200,200); p2.getX() 100 p2 = p1; p2.getX() 200 p1.setX(100); p2.getX() 100 Point p1 = new APoint(50, 50); P1P2 APoint@64APoint@8 Garbage collected Slide 15 == for Objects 200 APoint@8 64 200 16 48 8 Point p1 APoint@64 8Point p2 64 Point p2 = new APoint(200, 200) Point p1 = new APoint(200, 200); P1P2 APoint@64APoint@8 p2 == p2 false Same physical object? Slide 16 == for Objects 200 APoint@8 8 16 48 8 Point p1 8Point p2 Point p2 = p1; Point p1 = new APoint(200, 200); p2 == p2 true P1P2 APoint@8 Slide 17 == vs. Equal for Strings Joe Doe String@8 64 16 48 8 String s1 String@64 8String s2 64 String s2 = Joe Doe String s1 = Joe Doe; S1S2 String@64String@8 s1 == s2 false s1.equals(s2) true Slide 18 == vs. equals() for Objects 200 APoint@8 64 200 16 48 8 Point p1 APoint@64 8Point p2 64 Point p2 = new APoint(200, 200) Point p1 = new APoint(200, 200); p2 == p2 false public boolean equals(Point otherPoint) { return x == otherPoint.getX() && y == otherPoint.getY(); } p1.equals(p2) true Slide 19 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 Slide 20 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 Slide 21 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 Slide 22 Bounded Point Copy? public ABoundedPoint copy() { return new ABoundedPoint (x, y, upperLeftCorner, lowerRightCorner); }; Slide 23 Shallow Copy Slide 24 Deep Copy Slide 25 Bounded Point Deep Copy public ABoundedPoint copy() { return new ABoundedPoint (x, y, upperLeftCorner.copy(), lowerRightCorner.copy()); }; Slide 26 Shallow vs. Deep Copy Shallow copy: Copies the instance but not its components Deep copy Copies the instance and deep copies the components