Download presentation
Presentation is loading. Please wait.
1
Primitive Values Vs Objects Wrapper Classes Memory Representation of Primitive Values Vs Objects –Pointers Equal Vs == Sharing of Objects Garbage Collection
2
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
3
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
4
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;
5
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
6
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;} }
7
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)
8
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
9
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; … }
10
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);
11
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);
12
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;
13
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
14
== 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?
15
== 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
16
== 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
17
== 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.