1 / 17

Working with References

Working with References. 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. null References.

micol
Download Presentation

Working with References

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 nullreference • 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 doesn’t 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 doesn’t 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 don’t 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 s1==s3 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<b • 0 if a==b • a positive int if a>b • 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; … }

More Related