Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects vs. Primitives Primitive Primitive byte, short, int, long byte, short, int, long float, double float, double char char boolean boolean Object (instance.

Similar presentations


Presentation on theme: "Objects vs. Primitives Primitive Primitive byte, short, int, long byte, short, int, long float, double float, double char char boolean boolean Object (instance."— Presentation transcript:

1 Objects vs. Primitives Primitive Primitive byte, short, int, long byte, short, int, long float, double float, double char char boolean boolean Object (instance of a class) Object (instance of a class) String, Integer, Character, ArrayList, … String, Integer, Character, ArrayList, … Animal, Student, DVD, Room,.... Animal, Student, DVD, Room,....

2 Primitives private int x; private int x; x x = 17; Objects private Integer y; // y stores a null reference ! y [null] 0 17

3 Primitives private int x; x x = 17; Objects Integer y [null] y = new Integer (17); // y stores a reference to an object ! 17 0x00e12c ? private Integer y;

4 ArrayList Example ArrayList names; ArrayList names; names = new ArrayList(); names = new ArrayList(); names.add (new String (“joe”)); names.add (new String (“joe”)); names.add(new String (“pat”)); names.add(new String (“pat”)); names.add (new String (“androgynous”)); names.add (new String (“androgynous”)); names size: 0 joe pat androgynous 012 [null] 0x20410c ? size: 1size: 2size: 3

5 Assignment of References ArrayList kids; kids = names; kids.remove (1); names size: 0size: 2size: 1 joe pat androgynous size: 3 012 [null] 0x20410c ? kids [null] 0x20410c ?

6 Assignment of References ArrayList kids; kids = names; kids.remove (1); names size: 0size: 2size: 1 joe androgynous size: 3 01 [null] 0x20410c ? kids [null] 0x20410c ? size: 2

7 “Deep” Copy ArrayList kids = new ArrayList(); for (String aName : names) kids.add (aName); names size: 0size: 2size: 1 joe pat androgynous size: 3 012 0x20410c ? kids 0x204240 ? size: 0size: 1 0 aName

8 “Deep” Copy ArrayList kids = new ArrayList(); for (String aName : names) kids.add (aName); names size: 0size: 2size: 1 joe pat androgynous size: 3 012 0x20410c ? kids 0x204240 ? size: 2 01 aName

9 “Deep” Copy ArrayList kids = new ArrayList(); for (String aName : names) kids.add (aName); names size: 0size: 2size: 1 joe pat androgynous size: 3 012 0x20410c ? kids 0x204240 ? size: 3 012 aName

10 size: 2 size: 3 Automatic Garbage Collection names.remove (1); kids.remove (1); names size: 0size: 3size: 2 joe pat androgynous 012 0x20410c ? kids 0x204240 ? size: 3 012

11 References as Parameters // calling method [client] aMethod (names); names size: 3 joe pat androgynous 012 0x20410c ? kids 0x20410c ? size: 2 // called method [server] public void aMethod (List kids) { kids.remove (1); }

12 Comparison of Primitives int x, y; … if (x == y) … if (x != y) …

13 size: 3 Comparison of Objects if (names == kids) => false if (names.equals (kids)) => true names size: 3 joe pat androgynous 012 0x20410c ? kids 0x204240 ? size: 3 012 Moral: Always use.equals to compare objects.


Download ppt "Objects vs. Primitives Primitive Primitive byte, short, int, long byte, short, int, long float, double float, double char char boolean boolean Object (instance."

Similar presentations


Ads by Google