{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ch07 - 7 Objects and Collections CERTIFICATION OBJECTIVES...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
7 Objects and Collections CERTIFICATION OBJECTIVES Overriding hashCode() and equals() Collections Garbage Collection Two-Minute Drill Q&A Self Test
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CERTIFICATION OBJECTIVE Overriding hashCode() and equals() (Exam Objective 9.2) Distinguish between correct and incorrect implementations of hashcode methods. You’re an object. Get used to it. You have state, you have behavior, you have a job. (Or at least your chances of getting one will go up after passing the exam.) If you exclude primitives, everything in Java is an object. Not just object, but Object with a capital ‘O’. Every exception, every event, every array extends from java.lang.Object. We’ve already talked about it in Chapter 6 when we looked at overriding equals() , but there’s more to the story, and that more is what we’ll look at now. For the exam, you don’t need to know every method in Object, but you will need to know about the methods listed in Table 7-1. Chapter 9 covers wait() , notify() , and notifyAll() . The finalize() method is covered later in this chapter. So in this section we’ll look at just the hashCode() and equals() methods. Oh, that leaves out toString() , doesn’t it. OK, we’ll cover that right now because it takes two seconds. 2 Chapter 7: Objects and Collections Method Description boolean equals(Object obj) Decides whether two objects are meaningfully equivalent. void finalize() Called by the garbage collector when the garbage collector sees that the object cannot be referenced. int hashCode() Returns a hashcode int value for an object, so that the object can be used in Collection classes that use hashing, including Hashtable, HashMap, and HashSet. final void notify() Wakes up a thread that is waiting for this object’s lock. final void notifyAll() Wakes up all threads that are waiting for this object’s lock. final void wait() Causes the current thread to wait until another thread calls notify or notifyAll on this object. String toString() Returns a “text representation” of the object. TABLE 7-1 Methods of Class Object Covered on the Exam
Image of page 2
Overriding hashCode() and equals() (Exam Objective 9.2) 3 The toString() Method Override toString() when you want a mere mortal to be able to read something meaningful about the objects of your class. Code can call toString () on your object when it wants to read useful details about your object. For example, when you pass an object reference to the System.out.println() method, the object’s toString() method is called, and the return of toString() is what you see displayed as follows: public class HardToRead { public static void main (String [] args) { HardToRead h = new HardToRead(); System.out.println(h); } } Running the HardToRead class gives us the lovely and meaningful, % java HardToRead [email protected] The preceding output is what you get when you don’t override the toString() method of class Object. It gives you the class name (at least that’s meaningful) followed by the @ symbol, followed by the unsigned hexadecimal representation of the object’s hashcode.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern