Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Equality

The equals() method compares two objects for equality. Because equals() is a method of java.lang.Object, any object can be compared for equality to any other object. That is, every object at least inherits an equals() method.

The equals() method has this signature:

     public boolean equals(Object  obj)

The java.lang.Object.equals() method just checks to see if two reference variables refer to the same object. For example, consider the following three reference variables:

     Integer i1 = new Integer(7);
     Integer i2 = new Integer(7);
     Integer i3 = i2;

Using the equals() method in java.lang.Object, i1 is not equal to i2 because i1 and i2 refer to two different objects, even though those objects have the same value. On the other hand, i2 and i3 are considered equal to each other because they refer to the same object.

This is often not the behavior you want. Therefore, most classes override equals() with a method that is more appropriate to the specific class. For example, the equals() method in java.lang.Integer does in fact test the values of the Integer objects to see if they’re the same. Thus, i1.equals(i2) returns true because the equals() method in java.lang.Integer behaves differently than the equals() method in java.lang.Object.

It’s nonetheless important to realize that there are often multiple, sensible ways to decide whether two objects are equal. For example, consider these two URL objects:

     URL u1 = new URL("http://www.inch.com/");
     URL u2 = new URL("http://worm.inch.com/");

www.inch.com and worm.inch.com are different names for the same machine, so these URLs point to the same page. But are the URLs the same? Maybe not. After all, one of the host names could be moved to a different machine while the other one stayed behind. Or consider these two URLs:

     URL u3 = new URL("http://sunsite.unc.edu/javafaq/");
     URL u4 = new URL("http://calzone.oit.unc.edu/javafaq/");

These two URLs point to the same page on the Web. However the first URL goes over a 100 Megabit per second (Mbps) FDDI connection and the second over a 10 Mbps Ethernet connection. So these URLs are probably best considered to be unequal.

In fact, Java considers all four of the URLs above to be unequal. The equals() method in the URL class, like most of the equals() methods in the java packages, is relatively shallow. It does not attempt to discover whether two different objects might mean the same thing if they are superficially different.

This isn’t all. What about these two URLs?

     URL u5 = new URL("http://sunsite.unc.edu/javafaq/
      javafaq.html#xtocid1902930");
     URL u6 = new URL("http://sunsite.unc.edu/javafaq/
      javafaq.html#xtocid1902931");

These point to different sections of the same page. Although they are different in one sense, Java considers them to be equal because they point to the same page.

The bottom line is that there are few guarantees about how the equals() method behaves. The only thing you can be reasonably confident about is that references to the same object will be equal to each other. Otherwise, you have to do the best you can. Regrettably, the documentation for the equals() methods is often incomplete. The only real way to find out what a particular equals() method really does is to look at the source code. If that’s not possible, equals() methods tend to be simple enough to understand from decompiled or disassembled byte code. If for some reason you can’t disassemble or decompile a class, then you have to run as many tests as you can think of to determine what’s really going on.

Finalization

The finalize() method is called when an object is garbage-collected. The finalize()method is the programmer’s last chance to do something with an object. The finalize() method of the java.lang.Object class is an empty method; that is, it does absolutely nothing. It looks something like this:

     protected void finalize() throws Throwable {

     }

You may ask yourself, why even have a finalize() method in java.lang.Object if it never does anything? The reason is so that the runtime knows that it can always call an object’s finalize() method. A method doesn’t have to do anything to be called. Subclasses of java.lang.Object may override finalize(). If they do, their finalize() method is called. Otherwise the finalize() method in java.lang.Object is called. It’s much simpler to know you can always call finalize() for any object at all than to have to check whether an object has a finalize() method before calling it.

Runtime type information

In Java, an object can tell you what class it belongs to via the getClass() method of java.lang.Object, which has the following signature:

     public final Class getClass()

getClass() returns a Class object (an object of type Class) that can be manipulated with the methods of the last section.

There’s little reason to override this method in your own classes.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.