Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers Prasun Dewan Comp 114. Pointers Primitive values vs. objects Wrapper Classes Memory Representation of Primitive Values Vs Objects –Pointers Equal.

Similar presentations


Presentation on theme: "Pointers Prasun Dewan Comp 114. Pointers Primitive values vs. objects Wrapper Classes Memory Representation of Primitive Values Vs Objects –Pointers Equal."— Presentation transcript:

1 Pointers Prasun Dewan Comp 114

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

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

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

5 Storing Primitive Values/Variables double d 52 int i 80 double e int i = 5; memory address double d = 5.5; same size same, double size double e = d;

6 Storing Objects Values/Variables Integer I Double D Integer I = new Integer(5) Double D = new Double(5.5) memory address different sizes variables same size

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;} }

8 Structured Objects Integer I Point p Double D public class APoint implements Point { // instance vars int x, y; //methods … } Point p = new APoint(50, 100)

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

10 Inheritance public class APoint implements Point { int x, y; … } new ABoundedPoint(75, 75, new APoint(50,50), new APoint(100,100) ) ABounded public class ABoundedPoint extends APoint { Point upperLeftCorner ; Point lowerRightCorner; … }

11 Assignment of Object Variables Point p1 = new APoint(50, 50); Point p2 = p1; Point p Point p2 P1P2 p1.setX(100);

12 Assignment of Object Variables Point p1 = new APoint(50, 50); Point p2 = p1; Point p Point p2 p1.setX(100); p2.getX()  100 P1P2 p1 = new APoint(200,200);

13 Assignment of Object Variables Point p1 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 p2 = p1;

14 Assignment of Object Variables Point p1 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 Garbage collected

15 == for Objects Point p1 8Point p2 64 Point p2 = new APoint(200, 200) Point p1 = new APoint(200, 200); P1P2 p2 == p2  false Same physical object?

16 == for Objects Point p1 8Point p2 Point p2 = p1; Point p1 = new APoint(200, 200); p2 == p2  true P1P2

17 == vs. Equal for Strings Joe Doe String s1 8String s2 64 String s2 = “Joe Doe” String s1 = “Joe Doe”; S1S2 s1 == s2  false s1.equals(s2)  true

18 == vs. equals() for Objects Point p1 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

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

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

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

22 Bounded Point Copy? public ABoundedPoint copy() { return new ABoundedPoint (x, y, upperLeftCorner, lowerRightCorner); };

23 Shallow Copy

24 Deep Copy

25 Bounded Point Deep Copy public ABoundedPoint copy() { return new ABoundedPoint (x, y, upperLeftCorner.copy(), lowerRightCorner.copy()); };

26 Shallow vs. Deep Copy Shallow copy: –Copies the instance but not its components Deep copy –Copies the instance and deep copies the components


Download ppt "Pointers Prasun Dewan Comp 114. Pointers Primitive values vs. objects Wrapper Classes Memory Representation of Primitive Values Vs Objects –Pointers Equal."

Similar presentations


Ads by Google