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. package java.lang;
  8. /**
  9. * Class <code>Object</code> is the root of the class hierarchy.
  10. * Every class has <code>Object</code> as a superclass. All objects,
  11. * including arrays, implement the methods of this class.
  12. *
  13. * @author unascribed
  14. * @version 1.68, 04/08/04
  15. * @see java.lang.Class
  16. * @since JDK1.0
  17. */
  18. public class Object {
  19. private static native void registerNatives();
  20. static {
  21. registerNatives();
  22. }
  23. /**
  24. * Returns the runtime class of an object. That <tt>Class</tt>
  25. * object is the object that is locked by <tt>static synchronized</tt>
  26. * methods of the represented class.
  27. *
  28. * @return The <code>java.lang.Class</code> object that represents
  29. * the runtime class of the object. The result is of type
  30. * {@code Class<? extends X>} where X is the
  31. * erasure of the static type of the expression on which
  32. * <code>getClass</code> is called.
  33. */
  34. public final native Class<? extends Object> getClass();
  35. /**
  36. * Returns a hash code value for the object. This method is
  37. * supported for the benefit of hashtables such as those provided by
  38. * <code>java.util.Hashtable</code>.
  39. * <p>
  40. * The general contract of <code>hashCode</code> is:
  41. * <ul>
  42. * <li>Whenever it is invoked on the same object more than once during
  43. * an execution of a Java application, the <tt>hashCode</tt> method
  44. * must consistently return the same integer, provided no information
  45. * used in <tt>equals</tt> comparisons on the object is modified.
  46. * This integer need not remain consistent from one execution of an
  47. * application to another execution of the same application.
  48. * <li>If two objects are equal according to the <tt>equals(Object)</tt>
  49. * method, then calling the <code>hashCode</code> method on each of
  50. * the two objects must produce the same integer result.
  51. * <li>It is <em>not</em> required that if two objects are unequal
  52. * according to the {@link java.lang.Object#equals(java.lang.Object)}
  53. * method, then calling the <tt>hashCode</tt> method on each of the
  54. * two objects must produce distinct integer results. However, the
  55. * programmer should be aware that producing distinct integer results
  56. * for unequal objects may improve the performance of hashtables.
  57. * </ul>
  58. * <p>
  59. * As much as is reasonably practical, the hashCode method defined by
  60. * class <tt>Object</tt> does return distinct integers for distinct
  61. * objects. (This is typically implemented by converting the internal
  62. * address of the object into an integer, but this implementation
  63. * technique is not required by the
  64. * Java<font size="-2"><sup>TM</sup></font> programming language.)
  65. *
  66. * @return a hash code value for this object.
  67. * @see java.lang.Object#equals(java.lang.Object)
  68. * @see java.util.Hashtable
  69. */
  70. public native int hashCode();
  71. /**
  72. * Indicates whether some other object is "equal to" this one.
  73. * <p>
  74. * The <code>equals</code> method implements an equivalence relation
  75. * on non-null object references:
  76. * <ul>
  77. * <li>It is <i>reflexive</i>: for any non-null reference value
  78. * <code>x</code>, <code>x.equals(x)</code> should return
  79. * <code>true</code>.
  80. * <li>It is <i>symmetric</i>: for any non-null reference values
  81. * <code>x</code> and <code>y</code>, <code>x.equals(y)</code>
  82. * should return <code>true</code> if and only if
  83. * <code>y.equals(x)</code> returns <code>true</code>.
  84. * <li>It is <i>transitive</i>: for any non-null reference values
  85. * <code>x</code>, <code>y</code>, and <code>z</code>, if
  86. * <code>x.equals(y)</code> returns <code>true</code> and
  87. * <code>y.equals(z)</code> returns <code>true</code>, then
  88. * <code>x.equals(z)</code> should return <code>true</code>.
  89. * <li>It is <i>consistent</i>: for any non-null reference values
  90. * <code>x</code> and <code>y</code>, multiple invocations of
  91. * <tt>x.equals(y)</tt> consistently return <code>true</code>
  92. * or consistently return <code>false</code>, provided no
  93. * information used in <code>equals</code> comparisons on the
  94. * objects is modified.
  95. * <li>For any non-null reference value <code>x</code>,
  96. * <code>x.equals(null)</code> should return <code>false</code>.
  97. * </ul>
  98. * <p>
  99. * The <tt>equals</tt> method for class <code>Object</code> implements
  100. * the most discriminating possible equivalence relation on objects;
  101. * that is, for any non-null reference values <code>x</code> and
  102. * <code>y</code>, this method returns <code>true</code> if and only
  103. * if <code>x</code> and <code>y</code> refer to the same object
  104. * (<code>x == y</code> has the value <code>true</code>).
  105. * <p>
  106. * Note that it is generally necessary to override the <tt>hashCode</tt>
  107. * method whenever this method is overridden, so as to maintain the
  108. * general contract for the <tt>hashCode</tt> method, which states
  109. * that equal objects must have equal hash codes.
  110. *
  111. * @param obj the reference object with which to compare.
  112. * @return <code>true</code> if this object is the same as the obj
  113. * argument; <code>false</code> otherwise.
  114. * @see #hashCode()
  115. * @see java.util.Hashtable
  116. */
  117. public boolean equals(Object obj) {
  118. return (this == obj);
  119. }
  120. /**
  121. * Creates and returns a copy of this object. The precise meaning
  122. * of "copy" may depend on the class of the object. The general
  123. * intent is that, for any object <tt>x</tt>, the expression:
  124. * <blockquote>
  125. * <pre>
  126. * x.clone() != x</pre></blockquote>
  127. * will be true, and that the expression:
  128. * <blockquote>
  129. * <pre>
  130. * x.clone().getClass() == x.getClass()</pre></blockquote>
  131. * will be <tt>true</tt>, but these are not absolute requirements.
  132. * While it is typically the case that:
  133. * <blockquote>
  134. * <pre>
  135. * x.clone().equals(x)</pre></blockquote>
  136. * will be <tt>true</tt>, this is not an absolute requirement.
  137. * <p>
  138. * By convention, the returned object should be obtained by calling
  139. * <tt>super.clone</tt>. If a class and all of its superclasses (except
  140. * <tt>Object</tt>) obey this convention, it will be the case that
  141. * <tt>x.clone().getClass() == x.getClass()</tt>.
  142. * <p>
  143. * By convention, the object returned by this method should be independent
  144. * of this object (which is being cloned). To achieve this independence,
  145. * it may be necessary to modify one or more fields of the object returned
  146. * by <tt>super.clone</tt> before returning it. Typically, this means
  147. * copying any mutable objects that comprise the internal "deep structure"
  148. * of the object being cloned and replacing the references to these
  149. * objects with references to the copies. If a class contains only
  150. * primitive fields or references to immutable objects, then it is usually
  151. * the case that no fields in the object returned by <tt>super.clone</tt>
  152. * need to be modified.
  153. * <p>
  154. * The method <tt>clone</tt> for class <tt>Object</tt> performs a
  155. * specific cloning operation. First, if the class of this object does
  156. * not implement the interface <tt>Cloneable</tt>, then a
  157. * <tt>CloneNotSupportedException</tt> is thrown. Note that all arrays
  158. * are considered to implement the interface <tt>Cloneable</tt>.
  159. * Otherwise, this method creates a new instance of the class of this
  160. * object and initializes all its fields with exactly the contents of
  161. * the corresponding fields of this object, as if by assignment; the
  162. * contents of the fields are not themselves cloned. Thus, this method
  163. * performs a "shallow copy" of this object, not a "deep copy" operation.
  164. * <p>
  165. * The class <tt>Object</tt> does not itself implement the interface
  166. * <tt>Cloneable</tt>, so calling the <tt>clone</tt> method on an object
  167. * whose class is <tt>Object</tt> will result in throwing an
  168. * exception at run time.
  169. *
  170. * @return a clone of this instance.
  171. * @exception CloneNotSupportedException if the object's class does not
  172. * support the <code>Cloneable</code> interface. Subclasses
  173. * that override the <code>clone</code> method can also
  174. * throw this exception to indicate that an instance cannot
  175. * be cloned.
  176. * @see java.lang.Cloneable
  177. */
  178. protected native Object clone() throws CloneNotSupportedException;
  179. /**
  180. * Returns a string representation of the object. In general, the
  181. * <code>toString</code> method returns a string that
  182. * "textually represents" this object. The result should
  183. * be a concise but informative representation that is easy for a
  184. * person to read.
  185. * It is recommended that all subclasses override this method.
  186. * <p>
  187. * The <code>toString</code> method for class <code>Object</code>
  188. * returns a string consisting of the name of the class of which the
  189. * object is an instance, the at-sign character `<code>@</code>', and
  190. * the unsigned hexadecimal representation of the hash code of the
  191. * object. In other words, this method returns a string equal to the
  192. * value of:
  193. * <blockquote>
  194. * <pre>
  195. * getClass().getName() + '@' + Integer.toHexString(hashCode())
  196. * </pre></blockquote>
  197. *
  198. * @return a string representation of the object.
  199. */
  200. public String toString() {
  201. return getClass().getName() + "@" + Integer.toHexString(hashCode());
  202. }
  203. /**
  204. * Wakes up a single thread that is waiting on this object's
  205. * monitor. If any threads are waiting on this object, one of them
  206. * is chosen to be awakened. The choice is arbitrary and occurs at
  207. * the discretion of the implementation. A thread waits on an object's
  208. * monitor by calling one of the <code>wait</code> methods.
  209. * <p>
  210. * The awakened thread will not be able to proceed until the current
  211. * thread relinquishes the lock on this object. The awakened thread will
  212. * compete in the usual manner with any other threads that might be
  213. * actively competing to synchronize on this object; for example, the
  214. * awakened thread enjoys no reliable privilege or disadvantage in being
  215. * the next thread to lock this object.
  216. * <p>
  217. * This method should only be called by a thread that is the owner
  218. * of this object's monitor. A thread becomes the owner of the
  219. * object's monitor in one of three ways:
  220. * <ul>
  221. * <li>By executing a synchronized instance method of that object.
  222. * <li>By executing the body of a <code>synchronized</code> statement
  223. * that synchronizes on the object.
  224. * <li>For objects of type <code>Class,</code> by executing a
  225. * synchronized static method of that class.
  226. * </ul>
  227. * <p>
  228. * Only one thread at a time can own an object's monitor.
  229. *
  230. * @exception IllegalMonitorStateException if the current thread is not
  231. * the owner of this object's monitor.
  232. * @see java.lang.Object#notifyAll()
  233. * @see java.lang.Object#wait()
  234. */
  235. public final native void notify();
  236. /**
  237. * Wakes up all threads that are waiting on this object's monitor. A
  238. * thread waits on an object's monitor by calling one of the
  239. * <code>wait</code> methods.
  240. * <p>
  241. * The awakened threads will not be able to proceed until the current
  242. * thread relinquishes the lock on this object. The awakened threads
  243. * will compete in the usual manner with any other threads that might
  244. * be actively competing to synchronize on this object; for example,
  245. * the awakened threads enjoy no reliable privilege or disadvantage in
  246. * being the next thread to lock this object.
  247. * <p>
  248. * This method should only be called by a thread that is the owner
  249. * of this object's monitor. See the <code>notify</code> method for a
  250. * description of the ways in which a thread can become the owner of
  251. * a monitor.
  252. *
  253. * @exception IllegalMonitorStateException if the current thread is not
  254. * the owner of this object's monitor.
  255. * @see java.lang.Object#notify()
  256. * @see java.lang.Object#wait()
  257. */
  258. public final native void notifyAll();
  259. /**
  260. * Causes current thread to wait until either another thread invokes the
  261. * {@link java.lang.Object#notify()} method or the
  262. * {@link java.lang.Object#notifyAll()} method for this object, or a
  263. * specified amount of time has elapsed.
  264. * <p>
  265. * The current thread must own this object's monitor.
  266. * <p>
  267. * This method causes the current thread (call it <var>T</var>) to
  268. * place itself in the wait set for this object and then to relinquish
  269. * any and all synchronization claims on this object. Thread <var>T</var>
  270. * becomes disabled for thread scheduling purposes and lies dormant
  271. * until one of four things happens:
  272. * <ul>
  273. * <li>Some other thread invokes the <tt>notify</tt> method for this
  274. * object and thread <var>T</var> happens to be arbitrarily chosen as
  275. * the thread to be awakened.
  276. * <li>Some other thread invokes the <tt>notifyAll</tt> method for this
  277. * object.
  278. * <li>Some other thread {@link java.lang.Thread#interrupt() interrupts}
  279. * thread <var>T</var>.
  280. * <li>The specified amount of real time has elapsed, more or less. If
  281. * <tt>timeout</tt> is zero, however, then real time is not taken into
  282. * consideration and the thread simply waits until notified.
  283. * </ul>
  284. * The thread <var>T</var> is then removed from the wait set for this
  285. * object and re-enabled for thread scheduling. It then competes in the
  286. * usual manner with other threads for the right to synchronize on the
  287. * object; once it has gained control of the object, all its
  288. * synchronization claims on the object are restored to the status quo
  289. * ante - that is, to the situation as of the time that the <tt>wait</tt>
  290. * method was invoked. Thread <var>T</var> then returns from the
  291. * invocation of the <tt>wait</tt> method. Thus, on return from the
  292. * <tt>wait</tt> method, the synchronization state of the object and of
  293. * thread <tt>T</tt> is exactly as it was when the <tt>wait</tt> method
  294. * was invoked.
  295. * <p>
  296. * A thread can also wake up without being notified, interrupted, or
  297. * timing out, a so-called <i>spurious wakeup</i>. While this will rarely
  298. * occur in practice, applications must guard against it by testing for
  299. * the condition that should have caused the thread to be awakened, and
  300. * continuing to wait if the condition is not satisfied. In other words,
  301. * waits should always occur in loops, like this one:
  302. * <pre>
  303. * synchronized (obj) {
  304. * while (<condition does not hold>)
  305. * obj.wait(timeout);
  306. * ... // Perform action appropriate to condition
  307. * }
  308. * </pre>
  309. * (For more information on this topic, see Section 3.2.3 in Doug Lea's
  310. * "Concurrent Programming in Java (Second Edition)" (Addison-Wesley,
  311. * 2000), or Item 50 in Joshua Bloch's "Effective Java Programming
  312. * Language Guide" (Addison-Wesley, 2001).
  313. * <p>
  314. * If the current thread is
  315. * {@link java.lang.Thread#interrupt() interrupted} by another thread
  316. * while it is waiting, then an <tt>InterruptedException</tt> is thrown.
  317. * This exception is not thrown until the lock status of this object has
  318. * been restored as described above.
  319. * <p>
  320. * Note that the <tt>wait</tt> method, as it places the current thread
  321. * into the wait set for this object, unlocks only this object; any
  322. * other objects on which the current thread may be synchronized remain
  323. * locked while the thread waits.
  324. * <p>
  325. * This method should only be called by a thread that is the owner
  326. * of this object's monitor. See the <code>notify</code> method for a
  327. * description of the ways in which a thread can become the owner of
  328. * a monitor.
  329. *
  330. * @param timeout the maximum time to wait in milliseconds.
  331. * @exception IllegalArgumentException if the value of timeout is
  332. * negative.
  333. * @exception IllegalMonitorStateException if the current thread is not
  334. * the owner of the object's monitor.
  335. * @exception InterruptedException if another thread interrupted the
  336. * current thread before or while the current thread
  337. * was waiting for a notification. The <i>interrupted
  338. * status</i> of the current thread is cleared when
  339. * this exception is thrown.
  340. * @see java.lang.Object#notify()
  341. * @see java.lang.Object#notifyAll()
  342. */
  343. public final native void wait(long timeout) throws InterruptedException;
  344. /**
  345. * Causes current thread to wait until another thread invokes the
  346. * {@link java.lang.Object#notify()} method or the
  347. * {@link java.lang.Object#notifyAll()} method for this object, or
  348. * some other thread interrupts the current thread, or a certain
  349. * amount of real time has elapsed.
  350. * <p>
  351. * This method is similar to the <code>wait</code> method of one
  352. * argument, but it allows finer control over the amount of time to
  353. * wait for a notification before giving up. The amount of real time,
  354. * measured in nanoseconds, is given by:
  355. * <blockquote>
  356. * <pre>
  357. * 1000000*timeout+nanos</pre></blockquote>
  358. * <p>
  359. * In all other respects, this method does the same thing as the
  360. * method {@link #wait(long)} of one argument. In particular,
  361. * <tt>wait(0, 0)</tt> means the same thing as <tt>wait(0)</tt>.
  362. * <p>
  363. * The current thread must own this object's monitor. The thread
  364. * releases ownership of this monitor and waits until either of the
  365. * following two conditions has occurred:
  366. * <ul>
  367. * <li>Another thread notifies threads waiting on this object's monitor
  368. * to wake up either through a call to the <code>notify</code> method
  369. * or the <code>notifyAll</code> method.
  370. * <li>The timeout period, specified by <code>timeout</code>
  371. * milliseconds plus <code>nanos</code> nanoseconds arguments, has
  372. * elapsed.
  373. * </ul>
  374. * <p>
  375. * The thread then waits until it can re-obtain ownership of the
  376. * monitor and resumes execution.
  377. * <p>
  378. * As in the one argument version, interrupts and spurious wakeups are
  379. * possible, and this method should always be used in a loop:
  380. * <pre>
  381. * synchronized (obj) {
  382. * while (<condition does not hold>)
  383. * obj.wait(timeout, nanos);
  384. * ... // Perform action appropriate to condition
  385. * }
  386. * </pre>
  387. * This method should only be called by a thread that is the owner
  388. * of this object's monitor. See the <code>notify</code> method for a
  389. * description of the ways in which a thread can become the owner of
  390. * a monitor.
  391. *
  392. * @param timeout the maximum time to wait in milliseconds.
  393. * @param nanos additional time, in nanoseconds range
  394. * 0-999999.
  395. * @exception IllegalArgumentException if the value of timeout is
  396. * negative or the value of nanos is
  397. * not in the range 0-999999.
  398. * @exception IllegalMonitorStateException if the current thread is not
  399. * the owner of this object's monitor.
  400. * @exception InterruptedException if another thread interrupted the
  401. * current thread before or while the current thread
  402. * was waiting for a notification. The <i>interrupted
  403. * status</i> of the current thread is cleared when
  404. * this exception is thrown.
  405. */
  406. public final void wait(long timeout, int nanos) throws InterruptedException {
  407. if (timeout < 0) {
  408. throw new IllegalArgumentException("timeout value is negative");
  409. }
  410. if (nanos < 0 || nanos > 999999) {
  411. throw new IllegalArgumentException(
  412. "nanosecond timeout value out of range");
  413. }
  414. if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
  415. timeout++;
  416. }
  417. wait(timeout);
  418. }
  419. /**
  420. * Causes current thread to wait until another thread invokes the
  421. * {@link java.lang.Object#notify()} method or the
  422. * {@link java.lang.Object#notifyAll()} method for this object.
  423. * In other words, this method behaves exactly as if it simply
  424. * performs the call <tt>wait(0)</tt>.
  425. * <p>
  426. * The current thread must own this object's monitor. The thread
  427. * releases ownership of this monitor and waits until another thread
  428. * notifies threads waiting on this object's monitor to wake up
  429. * either through a call to the <code>notify</code> method or the
  430. * <code>notifyAll</code> method. The thread then waits until it can
  431. * re-obtain ownership of the monitor and resumes execution.
  432. * <p>
  433. * As in the one argument version, interrupts and spurious wakeups are
  434. * possible, and this method should always be used in a loop:
  435. * <pre>
  436. * synchronized (obj) {
  437. * while (<condition does not hold>)
  438. * obj.wait();
  439. * ... // Perform action appropriate to condition
  440. * }
  441. * </pre>
  442. * This method should only be called by a thread that is the owner
  443. * of this object's monitor. See the <code>notify</code> method for a
  444. * description of the ways in which a thread can become the owner of
  445. * a monitor.
  446. *
  447. * @exception IllegalMonitorStateException if the current thread is not
  448. * the owner of the object's monitor.
  449. * @exception InterruptedException if another thread interrupted the
  450. * current thread before or while the current thread
  451. * was waiting for a notification. The <i>interrupted
  452. * status</i> of the current thread is cleared when
  453. * this exception is thrown.
  454. * @see java.lang.Object#notify()
  455. * @see java.lang.Object#notifyAll()
  456. */
  457. public final void wait() throws InterruptedException {
  458. wait(0);
  459. }
  460. /**
  461. * Called by the garbage collector on an object when garbage collection
  462. * determines that there are no more references to the object.
  463. * A subclass overrides the <code>finalize</code> method to dispose of
  464. * system resources or to perform other cleanup.
  465. * <p>
  466. * The general contract of <tt>finalize</tt> is that it is invoked
  467. * if and when the Java<font size="-2"><sup>TM</sup></font> virtual
  468. * machine has determined that there is no longer any
  469. * means by which this object can be accessed by any thread that has
  470. * not yet died, except as a result of an action taken by the
  471. * finalization of some other object or class which is ready to be
  472. * finalized. The <tt>finalize</tt> method may take any action, including
  473. * making this object available again to other threads; the usual purpose
  474. * of <tt>finalize</tt>, however, is to perform cleanup actions before
  475. * the object is irrevocably discarded. For example, the finalize method
  476. * for an object that represents an input/output connection might perform
  477. * explicit I/O transactions to break the connection before the object is
  478. * permanently discarded.
  479. * <p>
  480. * The <tt>finalize</tt> method of class <tt>Object</tt> performs no
  481. * special action; it simply returns normally. Subclasses of
  482. * <tt>Object</tt> may override this definition.
  483. * <p>
  484. * The Java programming language does not guarantee which thread will
  485. * invoke the <tt>finalize</tt> method for any given object. It is
  486. * guaranteed, however, that the thread that invokes finalize will not
  487. * be holding any user-visible synchronization locks when finalize is
  488. * invoked. If an uncaught exception is thrown by the finalize method,
  489. * the exception is ignored and finalization of that object terminates.
  490. * <p>
  491. * After the <tt>finalize</tt> method has been invoked for an object, no
  492. * further action is taken until the Java virtual machine has again
  493. * determined that there is no longer any means by which this object can
  494. * be accessed by any thread that has not yet died, including possible
  495. * actions by other objects or classes which are ready to be finalized,
  496. * at which point the object may be discarded.
  497. * <p>
  498. * The <tt>finalize</tt> method is never invoked more than once by a Java
  499. * virtual machine for any given object.
  500. * <p>
  501. * Any exception thrown by the <code>finalize</code> method causes
  502. * the finalization of this object to be halted, but is otherwise
  503. * ignored.
  504. *
  505. * @throws Throwable the <code>Exception</code> raised by this method
  506. */
  507. protected void finalize() throws Throwable { }
  508. }