Java Reference
In-Depth Information
3.8. The
Object
Class
The
Object
class is the root of the class hierarchy. Every class directly or
indirectly extends
Object
and so a variable of type
Object
can refer to any
object, whether a class instance or an array. For example, the
Attr
class
can hold an attribute of any type, so its
value
field was declared to be
of type
Object
. Such a class cannot hold primitive types directly, but can
The
Object
class defines a number of methods that are inherited by all ob-
jects. These methods fall into two categories: general utility methods and
methods that support threads. Thread support is covered in
Chapter 14
.
This section describes the utility methods and how they affect classes.
The utility methods are:
public boolean
equals(Object obj)
Compares the receiving object and the object referenced by
obj
for equality, returning
true
if they have the same value and
false
if they don't. If you want to determine whether two ref-
erences refer to the same object, you can compare them using
==
and
!=
. The
equals
method is concerned with value equality.
The default implementation of
equals
in
Object
assumes that an
object is equal only to itself, by testing if
this == obj
.
public int
hashCode()
Returns a hash code for this object. Each object has a hash
code for use in hashtables. The default implementation returns
a value that is usually different for different objects. It is used
when storing objects in hashed collections, as described in
protected Object
clone()
throws CloneNotSupportedException