ch07 - 7 Objects and Collections CERTIFICATION OBJECTIVES...

Info iconThis 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 Self Test
Background image of page 1

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

View Full DocumentRight 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
Background 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 HardToRead@a47e0 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
Background image of page 3

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

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

Page1 / 50

ch07 - 7 Objects and Collections CERTIFICATION OBJECTIVES...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online