© a+ computer science - . in java, any variable that refers to an object is a reference variable....

28
© A+ Computer Science - www.apluscompsci.com

Upload: willa-eugenia-young

Post on 20-Jan-2018

228 views

Category:

Documents


0 download

DESCRIPTION

In Java, any variable that refers to an Object is a reference variable. The variable stores the memory address of the actual Object.

TRANSCRIPT

Page 1: © A+ Computer Science - . In Java, any variable that refers to an Object is a reference variable. The…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© 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…

© A+ Computer Science - www.apluscompsci.com