Object
Name
ObjectSynopsis
- Class Name:
java.lang.Object- Superclass:
- None
- Immediate Subclasses:
- Too many to be listed here
- Interfaces Implemented:
- None
- Availability:
- JDK 1.0 or later
Description
The Object class is the ultimate superclass of all other classes in Java. Because every other class is a subclass of Object, all of the methods accessible from Object are inherited by every other class. In other words, all objects in Java, including arrays, have access to implementations of the methods in Object.
The methods of Object provide some basic object functionality. The equals() method compares two objects for equality, while the hashCode() method returns a hashcode for an object. The getClass() method returns the Class object associated with an object. The wait(), notify(), and notifyAll() methods support thread synchronization for an object. The toString() method provides a string representation of an object.
Some of these methods should be overridden by subclasses of Object. For example, every class should provide its own implementation of the toString() method, so that it can provide an appropriate string representation.
Although it is possible to create an instance of the Object class, this is rarely done because it is more useful to create specialized objects. However, it is often useful to declare a variable that contains a reference to an Object because such a variable can contain a reference to an object of any other class.
Class Summary
public class java.lang.Object {
// Constructors public Object(); // Public Instance Methods public boolean equals(Object obj);
public final native Class getClass();
public native int hashCode();
public final native void notify();
public final native void notifyAll();
public String toString();
public final native void wait();
public final native void wait(long millis);
public final native void wait(long millis, int nanos); // Protected Instance Methods protected native Object clone(); protected void finalize() throws Throwable;
}
Constructors
Object
public Object()
- Description
- Creates an instance of the
Objectclass.
Public Instance Methods
equals
public boolean equals(Object obj)
- Parameters
-
obj- The object to be compared with this object.
- Returns
trueif the objects are equal;falseif they are not.- Description
- The
equals()method ofObjectreturnstrueif theobjparameter refers to the same object as the object this method is associated with. This is equivalent to using the==operator to compare two objects.Some classes, such as
String, override theequals()method to provide a comparison based on the contents of the two objects, rather than on the strict equality of the references. Any subclass can override theequals()method to implement an appropriate comparison, as long as the overriding method satisfies the following rules for an equivalence relation:- The method is reflexive : given a reference
x,x.equals(x)returnstrue. - The method is symmetric : given references
xandy,x.equals(y)returnstrueif and only ify.equals(x)returnstrue. - The method is transitive : given references
x,y, andz, ifx.equals(y)returnstrueandy.equals(z)returnstrue, thenx.equals(z)returnstrue. - The method is consistent : given references
xandy, multiple invocations ofx.equals(y)consistently returntrueor consistently returnfalse, provided that no information contained by the objects referenced byxorychanges. - A comparison with
nullreturnsfalse: given a referencexthat is non-null,x.equals(null)returnsfalse.
- The method is reflexive : given a reference
getClass
public final native Class getClass()
- Returns
- A reference to the
Classobject that describes the class of this object. - Description
- The
getClass()method ofObjectreturns theClassobject that describes the class of this object. This method isfinal, so it cannot be overridden by subclasses.
hashCode
public native int hashCode()
- Returns
- A relatively unique value that should be the same for all objects that are considered equal.
- Description
- The
hashCode()method ofObjectcalculates a hashcode value for this object. The method returns an integer value that should be relatively unique to the object. If theequals()method for the object bases its result on the contents of the object, thehashcode()method should also base its result on the contents. ThehashCode()method is provided for the benefit of hashtables, which store and retrieve elements using key values called hashcodes. The internal placement of a particular piece of data is determined by its hashcode; hashtables are designed to use hashcodes to provide efficient retrieval.The
java.util.Hashtableclass provides an implementation of a hashtable that stores values of typeObject. Each object is stored in the hashtable based on the hash code of its key object. It is important that each object have the most unique hash code possible. If two objects have the same hash code but they are not equal (as determined byequals()), aHashtablethat stores these two objects may need to spend additional time searching when it is trying to retrieve objects. The implementation ofhashCode()inObjecttries to make sure that every object has a distinct hash code by basing its result on the internal representation of a reference to the object.Some classes, such as
String, override thehashCode()method to produce values based on the contents of individual objects, instead of the objects themselves. In other words, twoStringobjects that contain the exact same strings have the same hash code. IfStringdid not override thehashCode()method inherited fromObject, these twoStringobjects would have different hash code values and it would be impossible to useStringobjects as keys for hashtables.Any subclass can override the
hashCode()method to implement an appropriate way of producing hash code values, as long as the overriding method satisfies the following rules:- If the
hashCode()method is called on the same object more than once during the execution of a Java application, it must consistently return the same integer value. The integer does not, however, need to be consistent between Java applications, or from one execution of an application to another execution of the same application. - If two objects compare as equal according to their
equals()methods, calls to thehashCode()methods for the objects must produce the same result. - If two objects compare as not equal according to their
equals()methods, calls to thehashCode()methods for the two objects are not required to produce distinct results. However, implementations ofhashCode()that produce distinct results for unequal objects may improve the performance of hashtables.
In general, if a subclass overrides the
equals()method ofObject, it should also override thehashCode()method. - If the
notify
public final native void notify()
- Throws
-
IllegalMonitorStateException- If the method is called from a thread that does not hold this object's lock.
- Description
- The
notify()method wakes up a thread that is waiting to return from a call to this object'swait()method. The awakened thread can resume executing as soon as it regains this object's lock. If more than one thread is waiting, thenotify()method arbitrarily awakens just one of the threads.The
notify()method can be called only by a thread that is the current owner of this object's lock. A thread holds the lock on this object while it is executing asynchronizedinstance method of the object or executing the body of asynchronizedstatement that synchronizes on the object. A thread can also hold the lock for aClassobject if it is executing asynchronizedstaticmethod of that class.This method is
final, so it cannot be overridden by subclasses.
notifyAll
public final native void notifyAll()
- Throws
-
IllegalMonitorStateException- If the method is called from a thread that does not hold this object's lock.
- Description
- The
notifyAll()method wakes up all the threads that are waiting to return from a call to this object'swait()method. Each awakened thread can resume executing as soon as it regains this object's lock.The
notifyAll()method can be called only by a thread that is the current owner of this object's lock. A thread holds the lock on this object while it is executing asynchronizedinstance method of the object or executing the body of asynchronizedstatement that synchronizes on the object. A thread can also hold the lock for aClassobject if it is executing asynchronizedstaticmethod of that class.This method is
final, so it cannot be overridden by subclasses.
toString
public String toString()
- Returns
- The string representation of this object.
- Description
- The
toString()method ofObjectreturns a generic string representation of this object. The method returns aStringthat consists of the object's class name, an "at" sign, and the unsigned hexadecimal representation of the value returned by the object'shashCode()method.Many classes override the
toString()method to provide a string representation that is specific to that type of object. Any subclass can override thetoString()method; the overriding method should simply return aStringthat represents the contents of the object with which it is associated.
wait
public final native void wait() throws InterruptedException
- Throws
-
IllegalMonitorStateException- If the method is called from a thread that does not hold this object's lock.
InterruptedException- If another thread interrupted this thread.
- Description
- The
wait()method causes a thread to wait until it is notified by another thread to stop waiting. Whenwait()is called, the thread releases its lock on this object and waits until another thread notifies it to wake up through a call to eithernotify()ornotifyAll(). After the thread is awakened, it has to regain the lock on this object before it can resume executing.The
wait()method can be called only by a thread that is the current owner of this object's lock. A thread holds the lock on this object while it is executing asynchronizedinstance method of the object or executing the body of asynchronizedstatement that synchronizes on the object. A thread can also hold the lock for aClassobject if it is executing asynchronizedstaticmethod of that class.This method is
final, so it cannot be overridden by subclasses.
public final native void wait(long timeout) throws InterruptedException
- Parameters
-
timeout- The maximum number of milliseconds to wait.
- Throws
-
IllegalMonitorStateException- If the method is called from a thread that does not hold this object's lock.
InterruptedException- If another thread interrupted this thread.
- Description
- The
wait()method causes a thread to wait until it is notified by another thread to stop waiting or until the specified amount of time has elapsed, whichever comes first. Whenwait()is called, the thread releases its lock on this object and waits until another thread notifies it to wake up through a call to eithernotify()ornotifyAll(). If the thread is not notified within the specifiedtimeoutperiod, it is automatically awakened when that amount of time has elapsed. Iftimeoutis zero, the thread waits indefinitely, just as ifwait()had been called without atimeoutargument. After the thread is awakened, it has to regain the lock on this object before it can resume executing.The
wait()method can be called only by a thread that is the current owner of this object's lock. A thread holds the lock on this object while it is executing asynchronizedinstance method of the object or executing the body of asynchronizedstatement that synchronizes on the object. A thread can also hold the lock for aClassobject if it is executing asynchronizedstaticmethod of that class.This method is
final, so it cannot be overridden by subclasses.
public final native void wait(long timeout, int nanos) throws InterruptedException
- Parameters
-
timeout- The maximum number of milliseconds to wait.
nanos- An additional number of nanoseconds to wait.
- Throws
-
IllegalMonitorStateException- If the method is called from a thread that does not hold this object's lock.
InterruptedException- If another thread interrupted this thread.
- Description
- The
wait()method causes a thread to wait until it is notified by another thread to stop waiting or until the specified amount of time has elapsed, whichever comes first. Whenwait()is called, the thread releases its lock on this object and waits until another thread notifies it to wake up through a call to eithernotify()ornotifyAll(). If the thread is not notified within the specified time period, it is automatically awakened when that amount of time has elapsed. Iftimeoutandnanosare zero, the thread waits indefinitely, just as ifwait()had been called without any arguments. After the thread is awakened, it has to regain the lock on this object before it can resume executing.The
wait()method can be called only by a thread that is the current owner of this object's lock. A thread holds the lock on this object while it is executing asynchronizedinstance method of the object or executing the body of asynchronizedstatement that synchronizes on the object. A thread can also hold the lock for aClassobject if it is executing asynchronizedstaticmethod of that class.Note that Sun's reference implementation of Java does not attempt to implement the precision implied by this method. Instead, it rounds to the nearest millisecond (unless
timeoutis0, in which case it rounds up to 1 millisecond) and callswait(long).This method is
final, so it cannot be overridden by subclasses.
Protected Instance Methods
clone
protected native Object clone() throws CloneNotSupportedException
- Returns
- A clone of this object.
- Throws
-
OutOfMemoryError- If there is not enough memory to create the new object.
CloneNotSupportedException- If the object is of a class that does not support
clone().
- Description
- A clone of an object is another object of the same type that has all of its instance variables set to the same values as the object being cloned. In other words, a clone is an exact copy of the original object.
The
clone()method ofObjectcreates a new object that is a clone of this object. No constructor is used in creating the clone. Theclone()method only clones an object if the class of that object indicates that its instances can be cloned. A class indicates that its objects can be cloned by implementing theCloneableinterface.Although array objects do not implement the
Cloneableinterface, theclone()method works for arrays. The clone of an array is an array that has the same number of elements as the original array, and each element in the clone array has the same value as the corresponding element in the original array. Note that if an array element contains an object reference, the clone array contains a reference to the same object, not a copy of the object.A subclass of
Objectcan override theclone()method inObjectto provide any additional functionality that is needed. For example, if an object contains references to other objects, theclone()method should recursively call theclone()methods of all the referenced objects. An overridingclone()method can throw aCloneNotSupportedExceptionto indicate that particular objects cannot be cloned.
finalize
protected void finalize() throws Throwable
- Throws
-
Throwable- For any reason that suits an overriding implementation of this method.
- Description
- The
finalize()method is called by the garbage collector when it decides that an object can never be referenced again. The method gives an object a chance to perform any cleanup operations that are necessary before it is destroyed by the garbage collector.The
finalize()method ofObjectdoes nothing. A subclass overrides thefinalize()method to perform any necessary cleanup operations. The overriding method should callsuper.finalize()as the very last thing it does, so that anyfinalize()method in the superclass is called.When the garbage collector calls an object's
finalize()method, the garbage collector does not immediately destroy the object because thefinalize()method might do something that results in a reference to the object. Thus the garbage collector waits to destroy the object until it can again prove it is safe to do so. The next time the garbage collector decides it is safe to destroy the object, it does so without callingfinalize()again. In other words, the garbage collector never calls thefinalize()method more than once for a particular object.A
finalize()method can throw any kind of exception. An exception causes thefinalize()method to stop running. The garbage collector then catches and ignores the exception, so it has no further effect on a program.
See Also
Equality Comparison Operators; Exceptions; Object Destruction; The finalize method; String; Threads 8; Throwable