Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Chap 07

Chap 07

Ratings: (0)|Views: 1 |Likes:
Published by api-3700560

More info:

Published by: api-3700560 on Oct 18, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Generics and
CertifiCation objeCtives
Design Using Collections
Override equals() and hashCode(),
Distinguish == and equals()
Use Generic Versions o\ue000 Collections
Including Set, List, and Map
Use Type Parameters,
Write Generics methods
Use java.util to Sort and Search
Use Comparable and Comparator
3 Two-Minute Drill
Q&ASel\ue000 Test
522Chapter 7: Generics and Collections
enerics are possibly the most talked about \ue000eature o\ue000 Java 5. Some people love 'em,
some people hate 'em, but they're here to stay. At their simplest, they can help make

code easier to write, and more robust. At their most complex, they can be very,
very hard to create, and maintain. Luckily, the exam creators stuck to the simple end o\ue000 generics,
covering the most common and use\ue000ul \ue000eatures, and leaving out most o\ue000 the especially tricky bits.
Coverage o\ue000 collections in this exam has expanded in two ways \ue000rom the previous exam: the use o\ue000
generics in collections, and the ability to sort and search through collections.

CertifiCation objeCtive
o\ue00d\ue002\ue00a\ue00a\ue005d\ue005\ue008\ue004 h\ue000\ue00bhC\ue009d\ue002() \ue000\ue008d \ue002qu\ue000\ue007\ue00b() (o\ue001\ue006\ue002c\ue00c\ue005\ue00d\ue002 6.2)
6.2 Distinguish between correct and incorrect overrides of corresponding hashCode and
equals methods, and explain the difference between == and the equals method.

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 anobject, but an Object
with a capital O. Every exception, every event, every array extends from
java.lang.Object. 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 coverswait(),notify(), andnotifyAll(). Thefinalize()
method was covered in Chapter 3. So in this section we'll look at just the
hashCode()and equals() methods. Oh, that leaves out toString(), doesn't it.
Okay, we'll cover that right now because it takes two seconds.
th\ue002 \ue00c\ue009s\ue00c\ue00a\ue005\ue008\ue004() M\ue002\ue00ch\ue009dOverridetoString() when you want a mere

mortal to be able to read something meaningful about the objects of your class. Code can calltoString() 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 oftoString() is shown in the following example:
Overriding hashCode() and equals() (Exam Objective 6.2)523
public class HardToRead {

public static void main (String [] args) {
HardToRead h = new HardToRead();

}Running the HardToRead class gives us the lovely and meaningful,
% java HardToRead

The preceding output is what you get when you don't override thetoString()
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.

Trying to read this output might motivate you to override thetoString()
method in your classes, for example,
public class BobTest {
public static void main (String[] args) {
Bob f = new Bob("GoBobGo", 19);
boolean equals (Object
Decides whether two objects are meaningfully equivalent.
void finalize()
Called by garbage collector when the garbage collector sees that the
object cannot be referenced.
int hashCode()

Returns a hashcodeint 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\u2019s lock.
final void notifyAll()
Wakes upall threads that are waiting for this object\u2019s lock.
final void wait()
Causes the current thread to wait until another thread calls
notify()or notifyAll() on this subject.
String toString()
Returns a \u201ctext representation\u201d of the object.
table 7-1
Methods o\ue000 Class Object Covered on the Exam

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->