Download presentation
Presentation is loading. Please wait.
1
Lecture 12: Using Classes Yoni Fridman 7/19/01 7/19/01
2
OutlineOutline ä How objects are stored The null keyword ä Assigning objects ä Garbage ä Examples ä How objects are stored The null keyword ä Assigning objects ä Garbage ä Examples
3
How Objects Are Stored Objects are stored differently from int ’s, double ’s, and boolean ’s An int variable, for example, stores a value. ä An object variable, on the other hand, stores a reference, not the object itself. Objects are stored differently from int ’s, double ’s, and boolean ’s An int variable, for example, stores a value. ä An object variable, on the other hand, stores a reference, not the object itself. int number = 10; number 10 Telephone myPhone = new Telephone(); myPhone on color volume number on color volume number false “Black” 5 942... Telephone object vs.
4
The null Keyword If we want to declare an object variable, but not have it point to an object yet, we can use the null keyword. ä Now the object variable myPhone exists, but we can’t do anything with it until it points to an object. If we want to declare an object variable, but not have it point to an object yet, we can use the null keyword. ä Now the object variable myPhone exists, but we can’t do anything with it until it points to an object. Telephone myPhone = null; myPhone
5
Assigning Objects Since objects are stored differently from int ’s (or double ’s or boolean ’s), they also act differently. Suppose we assign an int i to an int j : Now, let’s change the value of i : Since objects are stored differently from int ’s (or double ’s or boolean ’s), they also act differently. Suppose we assign an int i to an int j : Now, let’s change the value of i : int i = 10; int j = i; int i = 10; int j = i; i j i j 10 i = 20; i j i j 20 10
6
Assigning Objects This time, suppose we assign an object myPhone to an object otherPhone : Now, what happens if we call the method myPhone.turnOn() ? This time, suppose we assign an object myPhone to an object otherPhone : Now, what happens if we call the method myPhone.turnOn() ? Telephone myPhone = new Telephone(); Telephone otherPhone = myPhone; Telephone myPhone = new Telephone(); Telephone otherPhone = myPhone; myPhone otherPhone myPhone otherPhone on … on … false Telephone object
7
GarbageGarbage ä In this example, the first Tele- phone object is now garbage – it can never be accessed again. ä This can lead to memory leaks. ä To avoid memory leaks, Java performs garbage collection while your program is running. ä In this example, the first Tele- phone object is now garbage – it can never be accessed again. ä This can lead to memory leaks. ä To avoid memory leaks, Java performs garbage collection while your program is running. Telephone myPhone = new Telephone(); Telephone otherPhone = new Telephone(); myPhone = otherPhone; Telephone myPhone = new Telephone(); Telephone otherPhone = new Telephone(); myPhone = otherPhone; myPhone otherPhone myPhone otherPhone on … on … false on … on … false
8
HomeworkHomework ä NONE!!! (The reading in the textbook on input and text files bites.)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.