Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Working with References

2 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 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 doesn’t like null references  … but they can be hard to find for instance variables

4 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 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 6 The Picture Student s1 = new Student(300012345, “uid”); s1.setFirstName(“Sam”); s1 Student ----------- studentNumber = 300012345 firstName = “Sam” lastName = null...

7 7 Aliases Student s1 = new Student(300012345, “uid”); s1.setFirstName(“Sam”); Student s2 = s1; s1 s2 Student ----------- studentNumber = 300012345 firstName = “Sam” lastName = null...

8 8 Aliases s2.setFirstName(“Pat”); System.out.println(s1.getFirstName()); \\prints “Pat” s1 s2 Student ----------- studentNumber = 300012345 firstName = “Pat” lastName = null...

9 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 10 Immutable Object Reference For strings: String s1 = "Sam"; String s2 = s1; s2 = "Pat"; System.out.println(s1); System.out.println(s2);

11 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 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 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 s1 s2 Student ----------- 300012345 “uid” s3 Student ----------- 300012345 “uid”

14 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 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 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 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; … }


Download ppt "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."

Similar presentations


Ads by Google