170 likes | 308 Views
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.
E N D
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 • 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
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”
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
The Picture Student s1 = new Student(300012345, “uid”); s1.setFirstName(“Sam”); Student ----------- studentNumber = 300012345 firstName = “Sam” lastName = null ... s1
Aliases Student s1 = new Student(300012345, “uid”); s1.setFirstName(“Sam”); Student s2 = s1; Student ----------- studentNumber = 300012345 firstName = “Sam” lastName = null ... s1 s2
Aliases s2.setFirstName(“Pat”); System.out.println(s1.getFirstName()); \\prints “Pat” Student ----------- studentNumber = 300012345 firstName = “Pat” lastName = null ... s1 s2
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
Immutable Object Reference • For strings: String s1 = "Sam"; String s2 = s1; s2 = "Pat"; System.out.println(s1); System.out.println(s2);
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());
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; }
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
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; }
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
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
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; … }