© A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable....
-
Upload
willa-eugenia-young -
Category
Documents
-
view
228 -
download
0
description
Transcript of © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable....
![Page 1: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/1.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 2: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/2.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 3: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/3.jpg)
© A+ Computer Science - www.apluscompsci.com
In Java, any variable that refers to an Object is a reference variable.
The variable stores the memory address of the actual Object.
![Page 4: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/4.jpg)
© A+ Computer Science - www.apluscompsci.com
String x = new String("Chuck");String y = x;
x and y store the same memory address.
"Chuck"
x y0x2B3 0x2B30x2B3
![Page 5: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/5.jpg)
© A+ Computer Science - www.apluscompsci.com
String x = "Chuck";String y = "Chuck";
x and y store the same memory address.
"Chuck"
x y0x2B7 0x2B7 0x2B7
![Page 6: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/6.jpg)
© A+ Computer Science - www.apluscompsci.com
"Chuck""Chuck"
String x = new String("Chuck");String y = new String("Chuck");
x and y store different memory addresses.
x y0x2B7
0x2B7
0x2FE0x2FE
![Page 7: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/7.jpg)
© A+ Computer Science - www.apluscompsci.com
String x = "Chuck";String y = "Chuck";
"Chuck"
x y0x2B7 0x2B7 0x2B7
x = null;
null
![Page 8: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/8.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 9: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/9.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 10: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/10.jpg)
© A+ Computer Science - www.apluscompsci.com
String[] list = new String[50];//all 50 spots are null
0 1 2 3 4 5 6 7 . . . null null null null null null null null
![Page 11: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/11.jpg)
© A+ Computer Science - www.apluscompsci.com
0 1 2 3 4 5 6 7 . . . null null null 0x7 null null null null
"fred"
list[3] = "fred";
![Page 12: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/12.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 13: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/13.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 14: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/14.jpg)
© A+ Computer Science - www.apluscompsci.com
public class Monster{ // instance variables
public Monster(){ code } public Monster( int ht ) { code } public Monster(int ht, int wt) { code } public Monster(int ht, int wt, int age) { code }}
![Page 15: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/15.jpg)
© A+ Computer Science - www.apluscompsci.com
new Monster();
MONSTERProperties – height – 0 weight - 0 age - 0methods
m
m is a reference variable that refersto a Monster object.
Monster m =
![Page 16: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/16.jpg)
© A+ Computer Science - www.apluscompsci.com
Monster m = new Monster(23);
m is a reference variable that refersto a Monster object.
MONSTERProperties – height – 23 weight – 0 age - 0methods
m
0x234
0x234
![Page 17: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/17.jpg)
© A+ Computer Science - www.apluscompsci.com
Monster m = new Monster(23, 45);
m is a reference variable that refersto a Monster object.
MONSTERProperties – height – 23 weight – 45 age - 0methods
m0x239
0x239
![Page 18: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/18.jpg)
© A+ Computer Science - www.apluscompsci.com
Monster m = new Monster(23, 45, 11);
m is a reference variable that refersto a Monster object.
MONSTERProperties – height – 23 weight – 45 age - 11methods
m0x2B3
0x2B3
![Page 19: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/19.jpg)
© A+ Computer Science - www.apluscompsci.com
Monster[] list = new Monster[5];
out.println(list[0]);out.println(list[1]);out.println(list[2]);out.println(list[3]);out.println(list[4]);
OUTPUTnullnullnullnullnull
![Page 20: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/20.jpg)
© A+ Computer Science - www.apluscompsci.com
Monster[] list = new Monster[5];list[0] = new Monster();list[1] = new Monster(33);list[2] = new Monster(3,4,5);
out.println(list[0]);out.println(list[1]);out.println(list[2]);out.println(list[3]);
OUTPUT0 0 033 0 03 4 5null
![Page 21: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/21.jpg)
© A+ Computer Science - www.apluscompsci.com
0 1 2 3 4
MONSTERProperties – height – 0 weight – 0 age - 0methods
0x234
MONSTERProperties – height – 33 weight – 0 age - 0methods
0x238
MONSTERProperties – height – 3 weight – 4 age - 5methods
0x242
0x234 0x238 0x242 null null
![Page 22: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/22.jpg)
© A+ Computer Science - www.apluscompsci.com
0 1 2 3 4
MONSTERProperties – height – 0 weight – 0 age - 0methods
0x234
MONSTERProperties – height – 33 weight – 0 age - 0methods
0x238
MONSTERProperties – height – 3 weight – 4 age - 5methods
0x242
0x234 0x238 0x242 null null ray[2]=ray[1];0x238
![Page 23: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/23.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 24: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/24.jpg)
© A+ Computer Science - www.apluscompsci.com
public class Creature{ //data and constructors now shown
public void setSize(int girth){ size=girth; }
//toString not shown}
![Page 25: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/25.jpg)
© A+ Computer Science - www.apluscompsci.com
Creature[] creatures = new Creature[3];creatures[0]=new Creature(4);creatures[1]=new Creature(9);creatures[2]=new Creature(1);
out.println(creatures[0]);creatures[0].setSize(7);
out.println(creatures[0]);out.println(creatures[2]);
OUTPUT471
![Page 26: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/26.jpg)
© A+ Computer Science - www.apluscompsci.com
creatures[0]
What does the . dot do?
setSize(7);.
What does this store?
Creature
0x242
0x242
The . dot grants access to the Object at the stored address.
![Page 27: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/27.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 28: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…](https://reader036.fdocuments.us/reader036/viewer/2022062505/5a4d1c0f7f8b9ab0599f644d/html5/thumbnails/28.jpg)
© A+ Computer Science - www.apluscompsci.com