References in Java - Computer and Information Science
Transcript of References in Java - Computer and Information Science
![Page 1: References in Java - Computer and Information Science](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/1.jpg)
References in Java
CIS 110 (3/24/2021)
![Page 2: References in Java - Computer and Information Science](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/2.jpg)
For non-primitive types, variable assignment works by passing a reference.
![Page 3: References in Java - Computer and Information Science](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/3.jpg)
What’s a reference?
harry
firstName “Harry”
lastName “Smith”
age 24
positionTitle “Lecturer”This is!
![Page 4: References in Java - Computer and Information Science](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/4.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/5.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/6.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/7.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/8.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/9.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/10.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/11.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/12.jpg)
![Page 13: References in Java - Computer and Information Science](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/13.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/14.jpg)
![Page 15: References in Java - Computer and Information Science](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/15.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/16.jpg)
![Page 17: References in Java - Computer and Information Science](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/17.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/18.jpg)
Dereferencing
![Page 19: References in Java - Computer and Information Science](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/19.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/20.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/21.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/22.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/23.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/24.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/25.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/26.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/27.jpg)
![Page 28: References in Java - Computer and Information Science](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/28.jpg)
![Page 29: References in Java - Computer and Information Science](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/29.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/30.jpg)
And finally… STATIC!
![Page 31: References in Java - Computer and Information Science](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/31.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/32.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/33.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022012921/61c8fbf6b51fc21a362df952/html5/thumbnails/34.jpg)
Only static fields can be referenced inside of static methods.