100 likes | 163 Views
Explore the concept of 'null' reference in Java programming and how it can be used to manage object references effectively. Dive into examples and discussions on the importance of attending lectures for success in the course.
E N D
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu
Announcements • I did not lecture from slides. • I showed a demo of ‘green’ for UML class diagrams. • I showed a demo of the lab 4 solution. • We talked about ‘null’, the null reference. • We ended with a puzzle.
Attendance • I checked attendance today: • 72% of registered students attended • 28% of registered students did not attend • Lecture and lab attendance is closely linked with success in the course: • students who do NOT attend regularly tend to fail • students who do attend regularly tend to pass (with good grades)
‘null’ • ‘null’ denotes the null reference, a reference which does not refer to any object. • We can use ‘null’ to solve the two dogs, one collar problem (see code on next slide):
removeCollar rather than getCollar public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } public void setCollar(Collar collar) { _collar = collar; } public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; } }
Can also use in constructor public class Dog { private Collar _collar; public Dog() { _collar = null; } . . . } Now a Dog can be created without a Collar
Consider this code(assume association via constructor) fido rover 1025 825 Dog fido = new Dog(new Collar()); Dog rover = new Dog(new Collar()); 800 1000 _collar • _collar 800 825 1000 1025
Consider this code(assume association via constructor) fido rover temp 1025 825 Dog fido = new Dog(new Collar()); Dog rover = new Dog(new Collar()); fido.setCollar(rover.removeCollar()); public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; } 800 1000 _collar • _collar 800 825 1000 1025
Consider this code(assume association via constructor) fido rover temp 1025 825 Dog fido = new Dog(new Collar()); Dog rover = new Dog(new Collar()); fido.setCollar(rover.removeCollar()); public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; } 800 1000 _collar • _collar 800 825 1000 1025 What happens here? Which _collar are we referring to here?