Working with References 1 References n n n

















- Slides: 17
Working with References 1
References n n n 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 2
null References n A variable that does not currently point to anything is called a null reference q n // name is null We can check if a variable contains a null reference using the null identifier q n e. g. String name; e. g. if(name != null){…} Java doesn’t like null references q … but they can be hard to find for instance variables 3
null References n Look at Student. Reference. Test. java n Note: q q q 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 n it doesn’t know “where the reference has been” 4
null References n Three situations can arise: q q q 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 n null objects don’t have defined methods 5
The Picture Student s 1 = new Student(300012345, “uid”); s 1. set. First. Name(“Sam”); s 1 Student -----student. Number = 300012345 first. Name = “Sam” last. Name = null. . . 6
Aliases Student s 1 = new Student(300012345, “uid”); s 1. set. First. Name(“Sam”); Student s 2 = s 1; s 1 s 2 Student -----student. Number = 300012345 first. Name = “Sam” last. Name = null. . . 7
Aliases s 2. set. First. Name(“Pat”); System. out. println(s 1. get. First. Name()); \prints “Pat” s 1 s 2 Student -----student. Number = 300012345 first. Name = “Pat” last. Name = null. . . 8
Copying n n n 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 q e. g. String objects 9
Immutable Object Reference n For strings: String s 1 = "Sam"; String s 2 = s 1; s 2 = "Pat"; System. out. println(s 1); System. out. println(s 2); 10
Changeable Object Reference n For students: Student s 1 = new Student(1111, "u 1"); s 1. set. First. Name("Sam"); Student s 2 = s 1; s 2. set. First. Name("Pat"); System. out. println(s 1. get. First. Name()); System. out. println(s 2. get. First. Name()); 11
The clone Method n Many classes contain a clone() method q q n 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(student. Number, userid); //… copy the rest of the relevant data return s; } 12
Equality and References n Compare references not objects Student s 1 = Student s 2 = Student s 3 = \now s 1==s 3 new Student(300012345, ”uid”); s 1; and s 1!=s 2 Student -----300012345 “uid” s 1 s 2 Student -----300012345 “uid” s 3 13
The equals Method n Many classes define an equals method n “equal” depends on the class For students: n public boolean equals(Student s) { return student. Number == s. student. Number; } 14
The compare. To Method n n Used for more general comparison: <, ==, > a. compare. To(b) should return: q q q n Used by the built-in sorts q n a negative int if a<b 0 if a==b a positive int if a>b one call to compare. To gives all the info needed about the relative order For students… not 100% clear q q student. Number? But not all objects are sortable 15
The this Reference n n It is often convenient/necessary to explicitly refer to members of the current object q e. g. student. Number == s. student. Number q This can be confusing… same variable name The special identifier this refers to the object that the code is defining q e. g. this. student. Number == s. student. Number q This is more clear 16
Using this in Constructors n In constructors, we need to pass a formal parameter to fill a data member public Student(long stunum, …){ student. Number = stunum; … } n Using this can clarify the situation: public Student(long student. Number, …){ this. student. Number = student. Number; … } 17