darrell bethea may 24, 2011 - computer science€¦contain the memory address of the object named by...
TRANSCRIPT
![Page 1: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/1.jpg)
Darrell BetheaMay 24, 2011
![Page 2: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/2.jpg)
3
![Page 3: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/3.jpg)
Objects and references
More on Classes
4
![Page 4: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/4.jpg)
Classes Objects Instance variables Methods◦ Return types◦ Parameters and arguments
Information hiding and encapsulation◦ public/private◦ accessors/mutators
5
![Page 5: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/5.jpg)
Behave differently from variables of a primitive type
6
![Page 6: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/6.jpg)
When declaring a variable, a certain amount of memory is assigned based on the declared primitive type
What goes in this memory?
7
int age;
double length;
char letter;
memory
![Page 7: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/7.jpg)
A data value is stored in the location assigned to a variable of a primitive type
8
![Page 8: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/8.jpg)
What goes in these variables?
9
Student jack;
String inputString;
memory
![Page 9: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/9.jpg)
Contain the memory address of the object named by the variable◦ NOT the object itself
What is an address? Object is stored in some other location in
memory The address to this other location is called a
reference to the object Class types are also called reference types
10
![Page 10: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/10.jpg)
Assume we have a class named Book
Book jacksBook = new Book(“Java”);Book apusBook = new Book(“Java”);
vs.
Book jacksBook = new Book(“Java”);Book apusBook = jacksBook;
11
![Page 11: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/11.jpg)
12
Memory
??
...
...
...
![Page 12: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/12.jpg)
12
MemoryBook jacksBook;Book apusBook;jacksBook
apusBook ??
...
...
...
![Page 13: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/13.jpg)
12
MemoryBook jacksBook;Book apusBook;
jacksBook = new Book(“Java”);
jacksBookapusBook ?
“Java Book”?Book object
...
...
...
1056
1056
![Page 14: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/14.jpg)
12
MemoryBook jacksBook;Book apusBook;
jacksBook = new Book(“Java”);apusBook = new Book(“C++ Book”);
jacksBookapusBook
“Java Book”
“C++ Book”
?
?
Book object
Book object
...
...
...
1056
2078
10562078
![Page 15: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/15.jpg)
12
MemoryBook jacksBook;Book apusBook;
jacksBook = new Book(“Java”);apusBook = new Book(“C++ Book”);
jacksBook.setPage(68);
jacksBookapusBook
“Java Book”
“C++ Book”?
68Book object
Book object
...
...
...
1056
2078
10562078
![Page 16: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/16.jpg)
12
MemoryBook jacksBook;Book apusBook;
jacksBook = new Book(“Java”);apusBook = new Book(“C++ Book”);
jacksBook.setPage(68);apusBook.setPage(254);
jacksBookapusBook
“Java Book”
“C++ Book”254
68Book object
Book object
...
...
...
1056
2078
10562078
![Page 17: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/17.jpg)
12
MemoryBook jacksBook;Book apusBook;
jacksBook = new Book(“Java”);apusBook = new Book(“C++ Book”);
jacksBook.setPage(68);apusBook.setPage(254);
apusBook = jacksBook;
jacksBookapusBook
“Java Book”
“C++ Book”254
68Book object
Book object
...
...
...
1056
2078
10561056
![Page 18: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/18.jpg)
12
MemoryBook jacksBook;Book apusBook;
jacksBook = new Book(“Java”);apusBook = new Book(“C++ Book”);
jacksBook.setPage(68);apusBook.setPage(254);
apusBook = jacksBook;apusBook.setPage(316);
jacksBookapusBook
“Java Book”
“C++ Book”254
316Book object
Book object
...
...
...
1056
2078
10561056
![Page 19: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/19.jpg)
12
MemoryBook jacksBook;Book apusBook;
jacksBook = new Book(“Java”);apusBook = new Book(“C++ Book”);
jacksBook.setPage(68);apusBook.setPage(254);
apusBook = jacksBook;apusBook.setPage(316);
jacksBook is now on p. 316!
jacksBookapusBook
“Java Book”
“C++ Book”254
316Book object
Book object
...
...
...
1056
2078
10561056
![Page 20: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/20.jpg)
Variables of a class type contain memory addresses◦ NOT objects themselves
13
![Page 21: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/21.jpg)
String is a class type What happens when you have
String s1 = new String(“Hello”);String s2 = new String(“Hello”);boolean strEqual = (s1 == s2);
strEqual is false! Why? s1 and s2 store different addresses!
14
![Page 22: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/22.jpg)
What happens when you have
String s1 = new String(“Hello”);String s2 = new String(“Hello”);boolean strEqual = (s1.equals(s2));
strEqual is true! Why? String’s .equals() method checks if all the
characters in the two Strings are the same
15
![Page 23: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/23.jpg)
public class Book{ private String name; private int page;
public boolean equals(Book book) { return (this.name.equals(book.name) && this.page == book.page); }}
16
![Page 24: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/24.jpg)
Every class has a default .equals() method if it is not explicitly written◦ Does not necessarily do what you want
You decide what it means for two objects of a specific class type to be considered equal◦ Perhaps books are equal if the names and page
numbers are equal◦ Perhaps only if the names are equal◦ Put this logic inside .equals() method
17
![Page 25: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/25.jpg)
public void increaseNum(int num){ num++;}
public void doStuff(){ int x = 5; increaseNum(x); System.out.println(x);}
Prints 5. Why?
18
![Page 26: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/26.jpg)
public void increaseNum(int num){ num++;}
public void doStuff(){ int x = 5; increaseNum(x); System.out.println(x);}
Prints 5. Why?
18
num is local to increaseNum method does not change x
![Page 27: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/27.jpg)
public void changeBook(Book book){ book = new Book(“Biology”);}
public void doStuff(){ Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName());}
Prints Java. Why?
19
![Page 28: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/28.jpg)
public void changeBook(Book book){ book = new Book(“Biology”);}
public void doStuff(){ Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName());}
Prints Java. Why?
19
book is local to changeBook, does not change jacksBook
![Page 29: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/29.jpg)
public void changeBook(Book book){ book.setName(“Biology”);}
public void doStuff(){ Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName());}
Prints Biology. Why?
20
![Page 30: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/30.jpg)
public void changeBook(Book book){ book.setName(“Biology”);}
public void doStuff(){ Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName());}
Prints Biology. Why?
20
book contains the same address as jacksBook!
![Page 31: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/31.jpg)
Write an inventory class for pet store
It needs to have the ability to track◦ # of frogs◦ # of birds◦ # of turtles◦ 3 transaction types Add Remove Inquiry
21
![Page 32: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/32.jpg)
Would you like to make a transaction? y/nyWould you like to make a add (a), remove (r), or inquire (i)?aHow many frogs, birds, and turtles would you like to add?Frogs: 7Birds: 6Turtles: 4
Would you like to make a transaction? y/nyWould you like to make a add (a), remove (r), or inquire (i)?rHow many frogs, birds, and turtles would you like to remove?Frogs: 4Birds: 0Turtles: 0Would you like to make a transaction? y/nyWould you like to make a add (a), remove (r), or inquire (i)?iYou now have: 3 frogs6 birds4 turtles
22
![Page 33: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/33.jpg)
public class PetInventory { private int frogs = 0; private int birds = 0; private int turtles = 0;
24
Beginning of PetInventory Class
![Page 34: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/34.jpg)
getFrogs: int getBirds: int getTurtles: int
setFrogs(int) setBirds(int) setTurtles(int)
25
![Page 35: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/35.jpg)
public static void main(String[] args) { PetInventory petInventory = new PetInventory();
. . .
petInventory.add(5, 7, 9);
26
Add
![Page 36: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/36.jpg)
public static void main(String[] args) { PetInventory petInventory = new PetInventory();
. . .
petInventory.add(5, 7, 9);
public void add(int f, int b, int t){
frogs += f; birds += b; turtles += t;
}
26
Add
![Page 37: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/37.jpg)
public static void main(String[] args) { PetInventory petInventory = new PetInventory();
. . .
petInventory.add(5, 7, 9);
public void add(int f, int b, int t){
frogs += f; birds += b; turtles += t;
}
26
Add
![Page 38: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/38.jpg)
public static void main(String[] args) { PetInventory petInventory = new PetInventory();
. . .
petInventory.add(5, 7, 9);
public void add(int f, int b, int t){
frogs += f; birds += b; turtles += t;
}
26
Add
![Page 39: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/39.jpg)
public static void main(String[] args){
PetInventory petInventory = new PetInventory();
. . . petInventory.remove(5, 7, 9);
27
![Page 40: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/40.jpg)
public static void main(String[] args){
PetInventory petInventory = new PetInventory();
. . . petInventory.remove(5, 7, 9);
public void remove(int f, int b, int t){ if ((f > frogs) || (b > birds) || (t >
turtles)) { System.out.println("You do not have enough pets for that!"); } else { frogs -= f; birds -= b; turtles -= t; }
}
27
![Page 41: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/41.jpg)
public static void main(String[] args) { PetInventory petInventory = new PetInventory();
. . .
petInventory.inquire();
28
Inquire
![Page 42: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/42.jpg)
public void inquire() {
System.out.println("You now have: " + frogs + " frogs, " + birds + " birds, and " + turtles + " turtles.");
}
public static void main(String[] args) { PetInventory petInventory = new PetInventory();
. . .
petInventory.inquire();
28
Inquire
![Page 43: Darrell Bethea May 24, 2011 - Computer Science€¦Contain the memory address of the object named by the variable NOT the object itself What is an address? Object is stored in some](https://reader034.vdocument.in/reader034/viewer/2022050717/5e15738bf7e8657b76097156/html5/thumbnails/43.jpg)
Midterm Review
Bring questions about the Sample Midterm
29
Tomorrow