KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > lang > Object


1 /*
2  * @(#)Object.java 1.68 04/04/08
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.lang;
9
10 /**
11  * Class <code>Object</code> is the root of the class hierarchy.
12  * Every class has <code>Object</code> as a superclass. All objects,
13  * including arrays, implement the methods of this class.
14  *
15  * @author unascribed
16  * @version 1.68, 04/08/04
17  * @see java.lang.Class
18  * @since JDK1.0
19  */

20 public class Object {
21
22     private static native void registerNatives();
23     static {
24         registerNatives();
25     }
26
27     /**
28      * Returns the runtime class of an object. That <tt>Class</tt>
29      * object is the object that is locked by <tt>static synchronized</tt>
30      * methods of the represented class.
31      *
32      * @return The <code>java.lang.Class</code> object that represents
33      * the runtime class of the object. The result is of type
34      * {@code Class<? extends X>} where X is the
35      * erasure of the static type of the expression on which
36      * <code>getClass</code> is called.
37      */

38     public final native Class JavaDoc<? extends Object JavaDoc> getClass();
39
40     /**
41      * Returns a hash code value for the object. This method is
42      * supported for the benefit of hashtables such as those provided by
43      * <code>java.util.Hashtable</code>.
44      * <p>
45      * The general contract of <code>hashCode</code> is:
46      * <ul>
47      * <li>Whenever it is invoked on the same object more than once during
48      * an execution of a Java application, the <tt>hashCode</tt> method
49      * must consistently return the same integer, provided no information
50      * used in <tt>equals</tt> comparisons on the object is modified.
51      * This integer need not remain consistent from one execution of an
52      * application to another execution of the same application.
53      * <li>If two objects are equal according to the <tt>equals(Object)</tt>
54      * method, then calling the <code>hashCode</code> method on each of
55      * the two objects must produce the same integer result.
56      * <li>It is <em>not</em> required that if two objects are unequal
57      * according to the {@link java.lang.Object#equals(java.lang.Object)}
58      * method, then calling the <tt>hashCode</tt> method on each of the
59      * two objects must produce distinct integer results. However, the
60      * programmer should be aware that producing distinct integer results
61      * for unequal objects may improve the performance of hashtables.
62      * </ul>
63      * <p>
64      * As much as is reasonably practical, the hashCode method defined by
65      * class <tt>Object</tt> does return distinct integers for distinct
66      * objects. (This is typically implemented by converting the internal
67      * address of the object into an integer, but this implementation
68      * technique is not required by the
69      * Java<font size="-2"><sup>TM</sup></font> programming language.)
70      *
71      * @return a hash code value for this object.
72      * @see java.lang.Object#equals(java.lang.Object)
73      * @see java.util.Hashtable
74      */

75     public native int hashCode();
76
77     /**
78      * Indicates whether some other object is "equal to" this one.
79      * <p>
80      * The <code>equals</code> method implements an equivalence relation
81      * on non-null object references:
82      * <ul>
83      * <li>It is <i>reflexive</i>: for any non-null reference value
84      * <code>x</code>, <code>x.equals(x)</code> should return
85      * <code>true</code>.
86      * <li>It is <i>symmetric</i>: for any non-null reference values
87      * <code>x</code> and <code>y</code>, <code>x.equals(y)</code>
88      * should return <code>true</code> if and only if
89      * <code>y.equals(x)</code> returns <code>true</code>.
90      * <li>It is <i>transitive</i>: for any non-null reference values
91      * <code>x</code>, <code>y</code>, and <code>z</code>, if
92      * <code>x.equals(y)</code> returns <code>true</code> and
93      * <code>y.equals(z)</code> returns <code>true</code>, then
94      * <code>x.equals(z)</code> should return <code>true</code>.
95      * <li>It is <i>consistent</i>: for any non-null reference values
96      * <code>x</code> and <code>y</code>, multiple invocations of
97      * <tt>x.equals(y)</tt> consistently return <code>true</code>
98      * or consistently return <code>false</code>, provided no
99      * information used in <code>equals</code> comparisons on the
100      * objects is modified.
101      * <li>For any non-null reference value <code>x</code>,
102      * <code>x.equals(null)</code> should return <code>false</code>.
103      * </ul>
104      * <p>
105      * The <tt>equals</tt> method for class <code>Object</code> implements
106      * the most discriminating possible equivalence relation on objects;
107      * that is, for any non-null reference values <code>x</code> and
108      * <code>y</code>, this method returns <code>true</code> if and only
109      * if <code>x</code> and <code>y</code> refer to the same object
110      * (<code>x == y</code> has the value <code>true</code>).
111      * <p>
112      * Note that it is generally necessary to override the <tt>hashCode</tt>
113      * method whenever this method is overridden, so as to maintain the
114      * general contract for the <tt>hashCode</tt> method, which states
115      * that equal objects must have equal hash codes.
116      *
117      * @param obj the reference object with which to compare.
118      * @return <code>true</code> if this object is the same as the obj
119      * argument; <code>false</code> otherwise.
120      * @see #hashCode()
121      * @see java.util.Hashtable
122      */

123     public boolean equals(Object JavaDoc obj) {
124     return (this == obj);
125     }
126
127     /**
128      * Creates and returns a copy of this object. The precise meaning
129      * of "copy" may depend on the class of the object. The general
130      * intent is that, for any object <tt>x</tt>, the expression:
131      * <blockquote>
132      * <pre>
133      * x.clone() != x</pre></blockquote>
134      * will be true, and that the expression:
135      * <blockquote>
136      * <pre>
137      * x.clone().getClass() == x.getClass()</pre></blockquote>
138      * will be <tt>true</tt>, but these are not absolute requirements.
139      * While it is typically the case that:
140      * <blockquote>
141      * <pre>
142      * x.clone().equals(x)</pre></blockquote>
143      * will be <tt>true</tt>, this is not an absolute requirement.
144      * <p>
145      * By convention, the returned object should be obtained by calling
146      * <tt>super.clone</tt>. If a class and all of its superclasses (except
147      * <tt>Object</tt>) obey this convention, it will be the case that
148      * <tt>x.clone().getClass() == x.getClass()</tt>.
149      * <p>
150      * By convention, the object returned by this method should be independent
151      * of this object (which is being cloned). To achieve this independence,
152      * it may be necessary to modify one or more fields of the object returned
153      * by <tt>super.clone</tt> before returning it. Typically, this means
154      * copying any mutable objects that comprise the internal "deep structure"
155      * of the object being cloned and replacing the references to these
156      * objects with references to the copies. If a class contains only
157      * primitive fields or references to immutable objects, then it is usually
158      * the case that no fields in the object returned by <tt>super.clone</tt>
159      * need to be modified.
160      * <p>
161      * The method <tt>clone</tt> for class <tt>Object</tt> performs a
162      * specific cloning operation. First, if the class of this object does
163      * not implement the interface <tt>Cloneable</tt>, then a
164      * <tt>CloneNotSupportedException</tt> is thrown. Note that all arrays
165      * are considered to implement the interface <tt>Cloneable</tt>.
166      * Otherwise, this method creates a new instance of the class of this
167      * object and initializes all its fields with exactly the contents of
168      * the corresponding fields of this object, as if by assignment; the
169      * contents of the fields are not themselves cloned. Thus, this method
170      * performs a "shallow copy" of this object, not a "deep copy" operation.
171      * <p>
172      * The class <tt>Object</tt> does not itself implement the interface
173      * <tt>Cloneable</tt>, so calling the <tt>clone</tt> method on an object
174      * whose class is <tt>Object</tt> will result in throwing an
175      * exception at run time.
176      *
177      * @return a clone of this instance.
178      * @exception CloneNotSupportedException if the object's class does not
179      * support the <code>Cloneable</code> interface. Subclasses
180      * that override the <code>clone</code> method can also
181      * throw this exception to indicate that an instance cannot
182      * be cloned.
183      * @see java.lang.Cloneable
184      */

185     protected native Object JavaDoc clone() throws CloneNotSupportedException JavaDoc;
186
187     /**
188      * Returns a string representation of the object. In general, the
189      * <code>toString</code> method returns a string that
190      * "textually represents" this object. The result should
191      * be a concise but informative representation that is easy for a
192      * person to read.
193      * It is recommended that all subclasses override this method.
194      * <p>
195      * The <code>toString</code> method for class <code>Object</code>
196      * returns a string consisting of the name of the class of which the
197      * object is an instance, the at-sign character `<code>@</code>', and
198      * the unsigned hexadecimal representation of the hash code of the
199      * object. In other words, this method returns a string equal to the
200      * value of:
201      * <blockquote>
202      * <pre>
203      * getClass().getName() + '@' + Integer.toHexString(hashCode())
204      * </pre></blockquote>
205      *
206      * @return a string representation of the object.
207      */

208     public String JavaDoc toString() {
209     return getClass().getName() + "@" + Integer.toHexString(hashCode());
210     }
211
212     /**
213      * Wakes up a single thread that is waiting on this object's
214      * monitor. If any threads are waiting on this object, one of them
215      * is chosen to be awakened. The choice is arbitrary and occurs at
216      * the discretion of the implementation. A thread waits on an object's
217      * monitor by calling one of the <code>wait</code> methods.
218      * <p>
219      * The awakened thread will not be able to proceed until the current
220      * thread relinquishes the lock on this object. The awakened thread will
221      * compete in the usual manner with any other threads that might be
222      * actively competing to synchronize on this object; for example, the
223      * awakened thread enjoys no reliable privilege or disadvantage in being
224      * the next thread to lock this object.
225      * <p>
226      * This method should only be called by a thread that is the owner
227      * of this object's monitor. A thread becomes the owner of the
228      * object's monitor in one of three ways:
229      * <ul>
230      * <li>By executing a synchronized instance method of that object.
231      * <li>By executing the body of a <code>synchronized</code> statement
232      * that synchronizes on the object.
233      * <li>For objects of type <code>Class,</code> by executing a
234      * synchronized static method of that class.
235      * </ul>
236      * <p>
237      * Only one thread at a time can own an object's monitor.
238      *
239      * @exception IllegalMonitorStateException if the current thread is not
240      * the owner of this object's monitor.
241      * @see java.lang.Object#notifyAll()
242      * @see java.lang.Object#wait()
243      */

244     public final native void notify();
245
246     /**
247      * Wakes up all threads that are waiting on this object's monitor. A
248      * thread waits on an object's monitor by calling one of the
249      * <code>wait</code> methods.
250      * <p>
251      * The awakened threads will not be able to proceed until the current
252      * thread relinquishes the lock on this object. The awakened threads
253      * will compete in the usual manner with any other threads that might
254      * be actively competing to synchronize on this object; for example,
255      * the awakened threads enjoy no reliable privilege or disadvantage in
256      * being the next thread to lock this object.
257      * <p>
258      * This method should only be called by a thread that is the owner
259      * of this object's monitor. See the <code>notify</code> method for a
260      * description of the ways in which a thread can become the owner of
261      * a monitor.
262      *
263      * @exception IllegalMonitorStateException if the current thread is not
264      * the owner of this object's monitor.
265      * @see java.lang.Object#notify()
266      * @see java.lang.Object#wait()
267      */

268     public final native void notifyAll();
269
270     /**
271      * Causes current thread to wait until either another thread invokes the
272      * {@link java.lang.Object#notify()} method or the
273      * {@link java.lang.Object#notifyAll()} method for this object, or a
274      * specified amount of time has elapsed.
275      * <p>
276      * The current thread must own this object's monitor.
277      * <p>
278      * This method causes the current thread (call it <var>T</var>) to
279      * place itself in the wait set for this object and then to relinquish
280      * any and all synchronization claims on this object. Thread <var>T</var>
281      * becomes disabled for thread scheduling purposes and lies dormant
282      * until one of four things happens:
283      * <ul>
284      * <li>Some other thread invokes the <tt>notify</tt> method for this
285      * object and thread <var>T</var> happens to be arbitrarily chosen as
286      * the thread to be awakened.
287      * <li>Some other thread invokes the <tt>notifyAll</tt> method for this
288      * object.
289      * <li>Some other thread {@link java.lang.Thread#interrupt() interrupts}
290      * thread <var>T</var>.
291      * <li>The specified amount of real time has elapsed, more or less. If
292      * <tt>timeout</tt> is zero, however, then real time is not taken into
293      * consideration and the thread simply waits until notified.
294      * </ul>
295      * The thread <var>T</var> is then removed from the wait set for this
296      * object and re-enabled for thread scheduling. It then competes in the
297      * usual manner with other threads for the right to synchronize on the
298      * object; once it has gained control of the object, all its
299      * synchronization claims on the object are restored to the status quo
300      * ante - that is, to the situation as of the time that the <tt>wait</tt>
301      * method was invoked. Thread <var>T</var> then returns from the
302      * invocation of the <tt>wait</tt> method. Thus, on return from the
303      * <tt>wait</tt> method, the synchronization state of the object and of
304      * thread <tt>T</tt> is exactly as it was when the <tt>wait</tt> method
305      * was invoked.
306      * <p>
307      * A thread can also wake up without being notified, interrupted, or
308      * timing out, a so-called <i>spurious wakeup</i>. While this will rarely
309      * occur in practice, applications must guard against it by testing for
310      * the condition that should have caused the thread to be awakened, and
311      * continuing to wait if the condition is not satisfied. In other words,
312      * waits should always occur in loops, like this one:
313      * <pre>
314      * synchronized (obj) {
315      * while (&lt;condition does not hold&gt;)
316      * obj.wait(timeout);
317      * ... // Perform action appropriate to condition
318      * }
319      * </pre>
320      * (For more information on this topic, see Section 3.2.3 in Doug Lea's
321      * "Concurrent Programming in Java (Second Edition)" (Addison-Wesley,
322      * 2000), or Item 50 in Joshua Bloch's "Effective Java Programming
323      * Language Guide" (Addison-Wesley, 2001).
324      * <p>
325      * If the current thread is
326      * {@link java.lang.Thread#interrupt() interrupted} by another thread
327      * while it is waiting, then an <tt>InterruptedException</tt> is thrown.
328      * This exception is not thrown until the lock status of this object has
329      * been restored as described above.
330      * <p>
331      * Note that the <tt>wait</tt> method, as it places the current thread
332      * into the wait set for this object, unlocks only this object; any
333      * other objects on which the current thread may be synchronized remain
334      * locked while the thread waits.
335      * <p>
336      * This method should only be called by a thread that is the owner
337      * of this object's monitor. See the <code>notify</code> method for a
338      * description of the ways in which a thread can become the owner of
339      * a monitor.
340      *
341      * @param timeout the maximum time to wait in milliseconds.
342      * @exception IllegalArgumentException if the value of timeout is
343      * negative.
344      * @exception IllegalMonitorStateException if the current thread is not
345      * the owner of the object's monitor.
346      * @exception InterruptedException if another thread interrupted the
347      * current thread before or while the current thread
348      * was waiting for a notification. The <i>interrupted
349      * status</i> of the current thread is cleared when
350      * this exception is thrown.
351      * @see java.lang.Object#notify()
352      * @see java.lang.Object#notifyAll()
353      */

354     public final native void wait(long timeout) throws InterruptedException JavaDoc;
355
356     /**
357      * Causes current thread to wait until another thread invokes the
358      * {@link java.lang.Object#notify()} method or the
359      * {@link java.lang.Object#notifyAll()} method for this object, or
360      * some other thread interrupts the current thread, or a certain
361      * amount of real time has elapsed.
362      * <p>
363      * This method is similar to the <code>wait</code> method of one
364      * argument, but it allows finer control over the amount of time to
365      * wait for a notification before giving up. The amount of real time,
366      * measured in nanoseconds, is given by:
367      * <blockquote>
368      * <pre>
369      * 1000000*timeout+nanos</pre></blockquote>
370      * <p>
371      * In all other respects, this method does the same thing as the
372      * method {@link #wait(long)} of one argument. In particular,
373      * <tt>wait(0, 0)</tt> means the same thing as <tt>wait(0)</tt>.
374      * <p>
375      * The current thread must own this object's monitor. The thread
376      * releases ownership of this monitor and waits until either of the
377      * following two conditions has occurred:
378      * <ul>
379      * <li>Another thread notifies threads waiting on this object's monitor
380      * to wake up either through a call to the <code>notify</code> method
381      * or the <code>notifyAll</code> method.
382      * <li>The timeout period, specified by <code>timeout</code>
383      * milliseconds plus <code>nanos</code> nanoseconds arguments, has
384      * elapsed.
385      * </ul>
386      * <p>
387      * The thread then waits until it can re-obtain ownership of the
388      * monitor and resumes execution.
389      * <p>
390      * As in the one argument version, interrupts and spurious wakeups are
391      * possible, and this method should always be used in a loop:
392      * <pre>
393      * synchronized (obj) {
394      * while (&lt;condition does not hold&gt;)
395      * obj.wait(timeout, nanos);
396      * ... // Perform action appropriate to condition
397      * }
398      * </pre>
399      * This method should only be called by a thread that is the owner
400      * of this object's monitor. See the <code>notify</code> method for a
401      * description of the ways in which a thread can become the owner of
402      * a monitor.
403      *
404      * @param timeout the maximum time to wait in milliseconds.
405      * @param nanos additional time, in nanoseconds range
406      * 0-999999.
407      * @exception IllegalArgumentException if the value of timeout is
408      * negative or the value of nanos is
409      * not in the range 0-999999.
410      * @exception IllegalMonitorStateException if the current thread is not
411      * the owner of this object's monitor.
412      * @exception InterruptedException if another thread interrupted the
413      * current thread before or while the current thread
414      * was waiting for a notification. The <i>interrupted
415      * status</i> of the current thread is cleared when
416      * this exception is thrown.
417      */

418     public final void wait(long timeout, int nanos) throws InterruptedException JavaDoc {
419         if (timeout < 0) {
420             throw new IllegalArgumentException JavaDoc("timeout value is negative");
421         }
422
423         if (nanos < 0 || nanos > 999999) {
424             throw new IllegalArgumentException JavaDoc(
425                 "nanosecond timeout value out of range");
426         }
427
428     if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
429         timeout++;
430     }
431
432     wait(timeout);
433     }
434
435     /**
436      * Causes current thread to wait until another thread invokes the
437      * {@link java.lang.Object#notify()} method or the
438      * {@link java.lang.Object#notifyAll()} method for this object.
439      * In other words, this method behaves exactly as if it simply
440      * performs the call <tt>wait(0)</tt>.
441      * <p>
442      * The current thread must own this object's monitor. The thread
443      * releases ownership of this monitor and waits until another thread
444      * notifies threads waiting on this object's monitor to wake up
445      * either through a call to the <code>notify</code> method or the
446      * <code>notifyAll</code> method. The thread then waits until it can
447      * re-obtain ownership of the monitor and resumes execution.
448      * <p>
449      * As in the one argument version, interrupts and spurious wakeups are
450      * possible, and this method should always be used in a loop:
451      * <pre>
452      * synchronized (obj) {
453      * while (&lt;condition does not hold&gt;)
454      * obj.wait();
455      * ... // Perform action appropriate to condition
456      * }
457      * </pre>
458      * This method should only be called by a thread that is the owner
459      * of this object's monitor. See the <code>notify</code> method for a
460      * description of the ways in which a thread can become the owner of
461      * a monitor.
462      *
463      * @exception IllegalMonitorStateException if the current thread is not
464      * the owner of the object's monitor.
465      * @exception InterruptedException if another thread interrupted the
466      * current thread before or while the current thread
467      * was waiting for a notification. The <i>interrupted
468      * status</i> of the current thread is cleared when
469      * this exception is thrown.
470      * @see java.lang.Object#notify()
471      * @see java.lang.Object#notifyAll()
472      */

473     public final void wait() throws InterruptedException JavaDoc {
474     wait(0);
475     }
476
477     /**
478      * Called by the garbage collector on an object when garbage collection
479      * determines that there are no more references to the object.
480      * A subclass overrides the <code>finalize</code> method to dispose of
481      * system resources or to perform other cleanup.
482      * <p>
483      * The general contract of <tt>finalize</tt> is that it is invoked
484      * if and when the Java<font size="-2"><sup>TM</sup></font> virtual
485      * machine has determined that there is no longer any
486      * means by which this object can be accessed by any thread that has
487      * not yet died, except as a result of an action taken by the
488      * finalization of some other object or class which is ready to be
489      * finalized. The <tt>finalize</tt> method may take any action, including
490      * making this object available again to other threads; the usual purpose
491      * of <tt>finalize</tt>, however, is to perform cleanup actions before
492      * the object is irrevocably discarded. For example, the finalize method
493      * for an object that represents an input/output connection might perform
494      * explicit I/O transactions to break the connection before the object is
495      * permanently discarded.
496      * <p>
497      * The <tt>finalize</tt> method of class <tt>Object</tt> performs no
498      * special action; it simply returns normally. Subclasses of
499      * <tt>Object</tt> may override this definition.
500      * <p>
501      * The Java programming language does not guarantee which thread will
502      * invoke the <tt>finalize</tt> method for any given object. It is
503      * guaranteed, however, that the thread that invokes finalize will not
504      * be holding any user-visible synchronization locks when finalize is
505      * invoked. If an uncaught exception is thrown by the finalize method,
506      * the exception is ignored and finalization of that object terminates.
507      * <p>
508      * After the <tt>finalize</tt> method has been invoked for an object, no
509      * further action is taken until the Java virtual machine has again
510      * determined that there is no longer any means by which this object can
511      * be accessed by any thread that has not yet died, including possible
512      * actions by other objects or classes which are ready to be finalized,
513      * at which point the object may be discarded.
514      * <p>
515      * The <tt>finalize</tt> method is never invoked more than once by a Java
516      * virtual machine for any given object.
517      * <p>
518      * Any exception thrown by the <code>finalize</code> method causes
519      * the finalization of this object to be halted, but is otherwise
520      * ignored.
521      *
522      * @throws Throwable the <code>Exception</code> raised by this method
523      */

524     protected void finalize() throws Throwable JavaDoc { }
525 }
526
Popular Tags