Title: Primitive Values Vs Objects
1Primitive Values Vs Objects
- Wrapper Classes
- Memory Representation of Primitive Values Vs
Objects - Pointers
- Equal Vs
- Sharing of Objects
- Garbage Collection
2Wrapper Classes
Joe Doe.toString()
Object
Vector vector new Vector()
vector.addElement(Joe Doe)
5
new Integer(5)
(new Integer(5)).toString()
AStringHistory
String
vector.addElement(new Integer(5))
vector.elementAt(3)
vector.elementAt(3).intValue()
AStringDatabase
5.toString()
int
boolean
char
double
vector.addElement(5)
3Other 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
4Storing Primitive Values/Variables
int i 5
5.5
double d 5.5
5
double e d
5.5
5Storing Objects Values/Variables
Integer I new Integer(5)
Double D new Double(5.5)
8
16
6Structured 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
7Structured Objects
public class APoint implements Point //
instance vars int x, y //methods
16
Point p new APoint(50, 100)
80
8Inheritance
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
9Inheritance
public class APoint implements Point
int x, y
public class ABoundedPoint extends APoint
Point upperLeftCorner Point
lowerRightCorner
8
16
new ABoundedPoint(75, 75, new APoint(50,50),
new APoint(100,100) )
10Assignment of Object Variables
Point p1 new APoint(50, 50)
50
APoint_at_8
8
Point p2 p1
50
p1.setX(100)
8
8
11Assignment of Object Variables
Point p1 new APoint(50, 50)
100
APoint_at_8
8
Point p2 p1
50
p1.setX(100)
p2.getX()
?100
8
16
Point p1
p1 new APoint(200,200)
8
12Assignment of Object Variables
Point p1 new APoint(50, 50)
100
APoint_at_8
8
Point p2 p1
50
p1.setX(100)
p2.getX()
?100
64
16
Point p1
p1 new APoint(200,200)
p2.getX()
?100
p2 p1
48
8
Point p2
200
APoint_at_64
64
200
13Assignment of Object Variables
Point p1 new APoint(50, 50)
100
APoint_at_8
8
Point p2 p1
50
p1.setX(100)
p2.getX()
?100
64
16
Point p1
p1 new APoint(200,200)
p2.getX()
?100
p2 p1
48
64
Point p2
p2.getX()
?200
200
APoint_at_64
64
200
14 for Objects
Point p1 new APoint(200, 200)
200
APoint_at_8
8
Point p2 new APoint(200, 200)
200
p2 p2
? false
64
16
Point p1
48
8
Point p2
200
APoint_at_64
64
200
15 for Objects
Point p1 new APoint(200, 200)
200
APoint_at_8
8
Point p2 p1
200
p2 p2
? true
8
16
Point p1
48
8
Point p2
16 Vs Equal for Strings
String s1 Joe Doe
Joe Doe
String_at_8
8
String s2 Joe Doe
s1 s2
? false
64
16
String s1
s1.equals(s2)
? true
48
8
String s2
Joe Doe
String_at_64
64
17 Vs equals() for Objects
Point p1 new APoint(200, 200)
200
APoint_at_8
8
Point p2 new APoint(200, 200)
200
64
16
Point p1
48
8
Point p2
200
APoint_at_64
64
public boolean equals(Point otherPoint)
return x otherPoint.getX() y
otherPoint.getY()
200