Primitive Values Vs Objects Wrapper Classes Memory Representation of Primitive Values Vs Objects Pointers Equal Vs Sharing of Objects Garbage Collection Wrapper Classes Joe Doe toString Object String AStringHistory Number AStringDatabase Double Integer Vector vector new Vector vector addElement Joe Doe new new Integer 5 Integer 5 toString 5 vector addElement new Integer 5 vector elementAt 3 intValue vector elementAt 3 wrapper wrapper class class char boolean double int 5 toString vector addElement 5 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 Storing Primitive Values Variables 8 5 5 5 5 memory address 16 5 5 same size 48 5 5 double d 52 5 int i 80 5 5 double e int i 5 double d 5 5 same double size double e d Storing Objects Values Variables Integer I new Integer 5 5 5 Double 8 5 Integer 16 48 8 Double D 60 16 Integer I 8 Double D new Double 5 5 16 different sizes memory address variables same size 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 Structured Objects public class APoint implements Point 8 5 5 Double 8 instance vars 16 5 Integer 16 int x y methods Double D 48 60 16 Integer I 80 50 100 APoint 80 96 80 Point p new APoint 50 100 Point p 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 public class APoint implements Point Inheritance int x y 8 50 50 APoint 8 16 100 100 APoint 16 48 75 75 8 16 ABounded Point 48 public class ABoundedPoint extends APoint Point upperLeftCorner Point lowerRightCorner new ABoundedPoint 75 75 new APoint 50 50 new APoint 100 100 Assignment of Object Variables Point p1 new APoint 50 50 Point p2 p1 p1 setX 100 P1 P2 APoint 8 8 50 50 APoint 8 16 8 Point p1 48 8 Point p2 Assignment of Object Variables Point p1 new APoint 50 50 Point p2 p1 p1 setX 100 p2 getX 100 p1 new APoint 200 200 P1 P2 APoint 8 8 100 50 APoint 8 16 8 Point p1 48 8 Point p2 Assignment of Object Variables Point p1 new APoint 50 50 Point p2 p1 p1 setX 100 p2 getX 100 p1 new APoint 200 200 p2 getX 100 p2 p1 P1 APoint 64 P2 APoint 8 8 100 50 APoint 8 16 64 Point p1 48 8 Point p2 64 200 200 APoint 64 Assignment of Object Variables Point p1 new APoint 50 50 Point p2 p1 p1 setX 100 p2 getX 100 p1 new APoint 200 200 p2 getX 100 p2 p1 p2 getX 200 P1 APoint 64 P2 APoint 8 8 100 50 APoint 8 16 64 Point p1 48 64 Point p2 64 200 200 Garbage collected APoint 64 for Objects Point p1 new APoint 200 200 200 200 APoint 8 16 64 Point p1 48 8 Point p2 64 200 200 Point p2 new APoint 200 200 8 p2 p2 false Same physical object P1 APoint 64 P2 APoint 8 APoint 64 for Objects Point p1 new APoint 200 200 Point p2 p1 p2 p2 P1 8 200 200 APoint 8 16 8 Point p1 48 8 Point p2 true P2 APoint 8 Vs Equal for Strings String s1 Joe Doe String s2 Joe Doe s1 s2 8 Joe Doe String 8 16 64 String s1 48 8 String s2 64 Joe Doe String 64 false s1 equals s2 true S1 String 64 S2 String 8 Vs equals for Objects Point p1 new APoint 200 200 200 200 APoint 8 16 64 Point p1 48 8 Point p2 64 200 200 Point p2 new APoint 200 200 8 p2 p2 false p1 equals p2 true public boolean equals Point otherPoint return x otherPoint getX y otherPoint getY APoint 64
View Full Document