Working with References - PowerPoint PPT Presentation

About This Presentation
Title:

Working with References

Description:

e.g. String name; ... g. String objects. 10. Immutable Object Reference. For strings: String ... e.g. public Student clone() Student s = new Student ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 18
Provided by: aseR1
Category:

less

Transcript and Presenter's Notes

Title: Working with References


1
Working with References
2
References
  • Every object variable is a reference to an object
  • Also true when an object is passed as an argument
  • When the object is used, the reference is
    followed to find the actual object

3
null References
  • A variable that does not currently point to
    anything is called a null reference
  • e.g. String name // name is null
  • We can check if a variable contains a null
    reference using the null identifier
  • e.g. if(name ! null)
  • Java doesnt like null references
  • but they can be hard to find for instance
    variables

4
null References
  • Look at StudentReferenceTest.java
  • Note
  • Compiler catches null reference in the local
    variable
  • null references are never allowed in local
    variables
  • but the compiler can not catch the null
    reference in the instance variables
  • it doesnt know where the reference has been

5
null References
  • Three situations can arise
  • local variable null compile error
  • null reference printed/passed this can be
    missed initially
  • null reference used to call a method this gets
    a null pointer exception
  • null objects dont have defined methods

6
The Picture
  • Student s1 new Student(300012345, uid)
  • s1.setFirstName(Sam)

Student ----------- studentNumber
300012345 firstName Sam lastName null ...
s1
7
Aliases
  • Student s1 new Student(300012345, uid)
  • s1.setFirstName(Sam)
  • Student s2 s1

Student ----------- studentNumber
300012345 firstName Sam lastName null ...
s1
s2
8
Aliases
  • s2.setFirstName(Pat)
  • System.out.println(s1.getFirstName())
  • \\prints Pat

Student ----------- studentNumber
300012345 firstName Pat lastName null ...
s1
s2
9
Copying
  • If we really do want to copy an object, it has to
    be done manually
  • Create a new instance, and copy the relevant data
    over
  • Not an issue if there are no methods that change
    the object i.e. for immutable objects
  • e.g. String objects

10
Immutable Object Reference
  • For strings
  • String s1 "Sam"
  • String s2 s1
  • s2 "Pat"
  • System.out.println(s1)
  • System.out.println(s2)

11
Changeable Object Reference
  • For students
  • Student s1 new Student(11111111,"u1")
  • s1.setFirstName("Sam")
  • Student s2 s1
  • s2.setFirstName("Pat")
  • System.out.println(s1.getFirstName())
  • System.out.println(s2.getFirstName())

12
The clone Method
  • Many classes contain a clone() method
  • this returns a copy of the object
  • i.e. a new object with the relevant info copied
  • e.g.
  • public Student clone()
  • Student s new Student(studentNumber, userid)
  • // copy the rest of the relevant data
  • return s

13
Equality and References
  • Compare references not objects
  • Student s1 new Student(300012345,uid)
  • Student s2 new Student(300012345,uid)
  • Student s3 s1
  • \\now s1s3 and s1!s2

Student ----------- 300012345 uid
Student ----------- 300012345 uid
s1
s2
s3
14
The equals Method
  • Many classes define an equals method
  • equal depends on the class
  • For students
  • public boolean equals(Student s)
  • return studentNumber s.studentNumber

15
The compareTo Method
  • Used for more general comparison
  • a.compareTo(b) should return
  • a negative int if a
  • 0 if ab
  • a positive int if ab
  • Used by the built-in sorts
  • one call to compareTo gives all the info needed
    about the relative order
  • For students not 100 clear
  • studentNumber?
  • But not all objects are sortable

16
The this Reference
  • It is often convenient/necessary to explicitly
    refer to members of the current object
  • e.g. studentNumber s.studentNumber
  • This can be confusing same variable name
  • The special identifier this refers to the object
    that the code is defining
  • e.g. this.studentNumber s.studentNumber
  • This is more clear

17
Using this in Constructors
  • In constructors, we need to pass a formal
    parameter to fill a data member
  • public Student(long stunum, )
  • studentNumber stunum
  • Using this can clarify the situation
  • public Student(long studentNumber, )
  • this.studentNumber studentNumber
Write a Comment
User Comments (0)
About PowerShow.com