34
References in Java CIS 110 (3/24/2021)

References in Java - Computer and Information Science

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: References in Java - Computer and Information Science

References in Java

CIS 110 (3/24/2021)

Page 2: References in Java - Computer and Information Science

For non-primitive types, variable assignment works by passing a reference.

Page 3: References in Java - Computer and Information Science

What’s a reference?

harry

firstName “Harry”

lastName “Smith”

age 24

positionTitle “Lecturer”This is!

Page 4: References in Java - Computer and Information Science

What’s a reference?

harry

The variable

The pointee (aka the data, stored somewhere in the computer’s memory)

The reference (aka the pointer)

Instructor harry = new Instructor(“Harry”, “Smith”, 24, “Lecturer”);

firstName “Harry”

lastName “Smith”

age 24

positionTitle “Lecturer”

harry

Page 5: References in Java - Computer and Information Science

Why does the pointee look like this?

The pointee (aka the data, stored somewhere in the computer’s memory)

The pointee is a “box” containing the data that any object of its Class must have based on the fields (name, age, etc.)

firstName “Harry”

lastName “Smith”

age 24

positionTitle “Lecturer”

Page 6: References in Java - Computer and Information Science

Worked Example of Variable AssignmentComplex rootI = new Complex(0.0, 1.0);Complex posFour = new Complex(4.0, 0.0);

rootI

real 0.0

imag 1.0

posFour

real 4.0

imag 0.0

rootI

posFour

Page 7: References in Java - Computer and Information Science

Worked Example of Variable AssignmentComplex rootI = new Complex(0.0, 1.0);Complex posFour = new Complex(4.0, 0.0);

Complex zero = new Complex(0.0, 0.0); rootI

real 0.0

imag 1.0

posFour

real 4.0

imag 0.0

zero

real 0.0

imag 0.0

rootI

posFour

zero

Page 8: References in Java - Computer and Information Science

Worked Example of Variable AssignmentComplex rootI = new Complex(0.0, 1.0);Complex posFour = new Complex(4.0, 0.0);

Complex zero = new Complex(0.0, 0.0);zero = null;

rootI

real 0.0

imag 1.0

posFour

real 4.0

imag 0.0

zero

real 0.0

imag 0.0

rootI

posFour

zero

Page 9: References in Java - Computer and Information Science

Worked Example of Variable AssignmentComplex rootI = new Complex(0.0, 1.0);Complex posFour = new Complex(4.0, 0.0);

Complex zero = new Complex(0.0, 0.0);zero = null;

posFour.copyValues(rootI);

rootI

real 0.0

imag 1.0

posFour

real 0.0

imag 1.0

zero

rootI

posFour

zero

Page 10: References in Java - Computer and Information Science

Worked Example of Variable AssignmentComplex rootI = new Complex(0.0, 1.0);Complex posFour = new Complex(4.0, 0.0);

Complex zero = new Complex(0.0, 0.0);zero = null;

posFour.copyValues(rootI);

rootI

real 0.0

imag 1.0

posFour

real 0.0

imag 1.0

zero

rootI

posFour

zero

Page 11: References in Java - Computer and Information Science

Worked Example of Variable AssignmentComplex rootI = new Complex(0.0, 1.0);Complex posFour = new Complex(4.0, 0.0);

Complex zero = new Complex(0.0, 0.0);zero = null;

posFour.copyValues(rootI);

posFour == rootI; //How does this evaluate?

rootI

real 0.0

imag 1.0

posFour

real 0.0

imag 1.0

zero

rootI

posFour

zero

Page 12: References in Java - Computer and Information Science
Page 13: References in Java - Computer and Information Science

Worked Example of Variable AssignmentComplex rootI = new Complex(0.0, 1.0);Complex posFour = new Complex(4.0, 0.0);

Complex zero = new Complex(0.0, 0.0);zero = null;

posFour.copyValues(rootI);

posFour == rootI; // false

posFour.equals(rootI); // true or false?

rootI

real 0.0

imag 1.0

posFour

real 0.0

imag 1.0

zero

public boolean equals(Complex other) { return this.real == other.real && this.imag == other.imag;}

rootI

posFour

zero

Page 14: References in Java - Computer and Information Science
Page 15: References in Java - Computer and Information Science

Worked Example of Variable AssignmentComplex rootI = new Complex(0.0, 1.0);Complex posFour = new Complex(4.0, 0.0);

Complex zero = new Complex(0.0, 0.0);zero = null;

posFour.copyValues(rootI);

posFour == rootI; // false

posFour.equals(rootI); // true

posFour = rootI;

// how do these evaluate?posFour == rootI; posFour.equals(rootI);

rootI

real 0.0

imag 1.0

real 0.0

imag 1.0

rootI

posFour

zero

Page 16: References in Java - Computer and Information Science
Page 17: References in Java - Computer and Information Science

Worked Example of Variable AssignmentComplex rootI = new Complex(0.0, 1.0);Complex posFour = new Complex(4.0, 0.0);

Complex zero = new Complex(0.0, 0.0);zero = null;

posFour.copyValues(rootI);

posFour == rootI; // false

posFour.equals(rootI); // true

posFour = rootI;

// how do these evaluate?posFour == rootI; // trueposFour.equals(rootI); // true

rootI

real 0.0

imag 1.0

posFour

zero

rootIrootI

posFour

zero

Page 18: References in Java - Computer and Information Science

Dereferencing

Page 19: References in Java - Computer and Information Science

Dereferencing uses the . operator

harry

Instructor harry = new Instructor(“Harry”, “Smith”, 24, “Lecturer”);

System.out.println(harry.lastName + “, “ + harry.firstName)

firstName “Harry”

lastName “Smith”

age 24

positionTitle “Lecturer”

harry is the variable

For harry.lastName...

harry. tells Java to follow the reference stored inside of the variable

harry

Page 20: References in Java - Computer and Information Science

Dereferencing uses the . operator

harry

Instructor harry = new Instructor(“Harry”, “Smith”, 24, “Lecturer”);

System.out.println(harry.lastName + “, “ + harry.firstName)

firstName “Harry”

lastName “Smith”

age 24

positionTitle “Lecturer”

harry is the variable

For harry.lastName...

harry. tells Java to follow the reference stored inside of the variable

harry.lastName is the value of the field “lastName” of the object that we’ve arrived at following the reference.

harry

Page 21: References in Java - Computer and Information Science

Dereferencing uses the . operator

harry

Instructor harry = new Instructor(“Harry”, “Smith”, 24, “Lecturer”);

System.out.println(harry.lastName + “, “ + harry.firstName)

firstName “Harry”

lastName “Smith”

age 24

positionTitle “Lecturer”

harry

Page 22: References in Java - Computer and Information Science

Dereferencing uses the . operator

harry

Instructor harry = new Instructor(“Harry”, “Smith”, 24, “Lecturer”);

System.out.println(harry.lastName + “, “ + harry.firstName)

firstName “Harry”

lastName “Smith”

age 24

positionTitle “Lecturer”

harry

Page 23: References in Java - Computer and Information Science

Dereferencing can be repeated

Replace the age field with the Instructor’s department chair...

firstName “Harry”

lastName “Smith”

chair

positionTitle “Lecturer”

firstName “Zachary”

lastName “Ives”

chair

positionTitle “Professor”

harry

Page 24: References in Java - Computer and Information Science

Dereferencing can be repeatedharry.chair.lastName

firstName “Harry”

lastName “Smith”

chair

positionTitle “Lecturer”

firstName “Zachary”

lastName “Ives”

chair

positionTitle “Professor”

harry

harry is the variable

harry. follows the reference

Page 25: References in Java - Computer and Information Science

Dereferencing can be repeatedharry.chair.lastName

firstName “Harry”

lastName “Smith”

chair

positionTitle “Lecturer”

firstName “Zachary”

lastName “Ives”

chair

positionTitle “Professor”

harry

harry is the variable

harry. follows the reference

harry.chair stores a reference

harry.chair. follows this ref.

Page 26: References in Java - Computer and Information Science

Dereferencing can be repeatedharry.chair.lastName

firstName “Harry”

lastName “Smith”

chair

positionTitle “Lecturer”

firstName “Zachary”

lastName “Ives”

chair

positionTitle “Professor”

harry

harry is the variable

harry. follows the reference

harry.chair stores a reference

harry.chair. follows this ref.

Page 27: References in Java - Computer and Information Science
Page 28: References in Java - Computer and Information Science
Page 29: References in Java - Computer and Information Science

Dereferencing a null pointer gives a NullPointerException

harry.chair.chair.lastName

firstName “Harry”

lastName “Smith”

chair

positionTitle “Lecturer”

firstName “Zachary”

lastName “Ives”

chair

positionTitle “Professor”

harry

harry.chair.chair is null, so harry.chair.chair.lastName asks Java to follow a reference that doesn’t exist!

Page 30: References in Java - Computer and Information Science

And finally… STATIC!

Page 31: References in Java - Computer and Information Science

Static fields and methods belong to the entire Class. All objects of that Class share its same Static values.

Page 32: References in Java - Computer and Information Science

institution

firstName “Harry”

lastName “Smith”

chair

positionTitle “Lecturer”

institution

firstName “Zachary”

lastName “Ives”

chair

positionTitle “Professor”

harry

Instructor

institution “Penn”

Page 33: References in Java - Computer and Information Science

institution

firstName “Harry”

lastName “Smith”

chair

positionTitle “Lecturer”

institution

firstName “Zachary”

lastName “Ives”

chair

positionTitle “Professor”

harry

Instructor

institution “Penn”

These dashed arrows are not technically references, but this mental model should be useful.

Page 34: References in Java - Computer and Information Science

Only static fields can be referenced inside of static methods.