Download presentation
Presentation is loading. Please wait.
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
(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)
4
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
5
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
6
Storing Objects Values/Variables
5.5 8 Integer I = new Integer(5) different sizes Double D = new Double(5.5) 5 16 48 Double D 8 variables same size memory address 60 Integer I 16
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 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 8 5 16 48 Double D 60 Integer I 16 Point p = new APoint(50, 100) 50 100 80 96 Point p 80
9
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
10
Inheritance 50 APoint@8 8 100 16 APoint@16 48 ABoundedPoint@48 75 8 16
public class APoint implements Point { int x, y; … } 50 8 100 16 public class ABoundedPoint extends APoint { Point upperLeftCorner ; Point lowerRightCorner; … } 48 75 8 16 new ABoundedPoint(75, 75, new APoint(50,50), new APoint(100,100) )
11
Assignment of Object Variables
Point p1 = new APoint(50, 50); 8 50 Point p2 = p1; 50 p1.setX(100); 16 Point p1 8 P1 P2 48 Point p2 8
12
Assignment of Object Variables
Point p1 = new APoint(50, 50); 8 100 Point p2 = p1; 50 p1.setX(100); p2.getX() 100 16 8 Point p1 p1 = new APoint(200,200); P1 P2 48 Point p2 8
13
Assignment of Object Variables
Point p1 = new APoint(50, 50); 8 100 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 64 200 200
14
Assignment of Object Variables
Point p1 = new APoint(50, 50); 8 100 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 64 200 200 Garbage collected
15
== 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 64 200 200
16
== 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 48 8 Point p2
17
== 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 64 Joe Doe
18
== vs. equals() for Objects
Point p1 = new APoint(200, 200); 8 200 Point p2 = new APoint(200, 200) 200 p2 == p2 false 16 64 Point p1 p1.equals(p2) true 48 8 Point p2 64 200 public boolean equals(Point otherPoint) { return x == otherPoint.getX() && y == otherPoint.getY(); } 200
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: Deep copy
Copies the instance but not its components Deep copy Copies the instance and deep copies the components
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.