1. /*
  2. * @(#)Object.java 1.54 00/02/02
  3. *
  4. * Copyright 1994-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.lang;
  11. /**
  12. * Class <code>Object</code> is the root of the class hierarchy.
  13. * Every class has <code>Object</code> as a superclass. All objects,
  14. * including arrays, implement the methods of this class.
  15. *
  16. * @author unascribed
  17. * @version 1.54, 02/02/00
  18. * @see java.lang.Class
  19. * @since JDK1.0
  20. */
  21. public class Object {
  22. private static native void registerNatives();
  23. static {
  24. registerNatives();
  25. }
  26. /**
  27. * Returns the runtime class of an object. That <tt>Class</tt>
  28. * object is the object that is locked by <tt>static synchronized</tt>
  29. * methods of the represented class.
  30. *
  31. * @return the object of type <code>Class</code> that represents the
  32. * runtime class of the object.
  33. */
  34. public final native Class 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. * <ul>
  76. * <li>It is <i>reflexive</i>: for any reference value <code>x</code>,
  77. * <code>x.equals(x)</code> should return <code>true</code>.
  78. * <li>It is <i>symmetric</i>: for any reference values <code>x</code> and
  79. * <code>y</code>, <code>x.equals(y)</code> should return
  80. * <code>true</code> if and only if <code>y.equals(x)</code> returns
  81. * <code>true</code>.
  82. * <li>It is <i>transitive</i>: for any reference values <code>x</code>,
  83. * <code>y</code>, and <code>z</code>, if <code>x.equals(y)</code>
  84. * returns <code>true</code> and <code>y.equals(z)</code> returns
  85. * <code>true</code>, then <code>x.equals(z)</code> should return
  86. * <code>true</code>.
  87. * <li>It is <i>consistent</i>: for any reference values <code>x</code>
  88. * and <code>y</code>, multiple invocations of <tt>x.equals(y)</tt>
  89. * consistently return <code>true</code> or consistently return
  90. * <code>false</code>, provided no information used in
  91. * <code>equals</code> comparisons on the object is modified.
  92. * <li>For any non-null reference value <code>x</code>,
  93. * <code>x.equals(null)</code> should return <code>false</code>.
  94. * </ul>
  95. * <p>
  96. * The <tt>equals</tt> method for class <code>Object</code> implements
  97. * the most discriminating possible equivalence relation on objects;
  98. * that is, for any reference values <code>x</code> and <code>y</code>,
  99. * this method returns <code>true</code> if and only if <code>x</code> and
  100. * <code>y</code> refer to the same object (<code>x==y</code> has the
  101. * value <code>true</code>).
  102. *
  103. * @param obj the reference object with which to compare.
  104. * @return <code>true</code> if this object is the same as the obj
  105. * argument; <code>false</code> otherwise.
  106. * @see java.lang.Boolean#hashCode()
  107. * @see java.util.Hashtable
  108. */
  109. public boolean equals(Object obj) {
  110. return (this == obj);
  111. }
  112. /**
  113. * Creates and returns a copy of this object. The precise meaning
  114. * of "copy" may depend on the class of the object. The general
  115. * intent is that, for any object <tt>x</tt>, the expression:
  116. * <blockquote>
  117. * <pre>
  118. * x.clone() != x</pre></blockquote>
  119. * will be true, and that the expression:
  120. * <blockquote>
  121. * <pre>
  122. * x.clone().getClass() == x.getClass()</pre></blockquote>
  123. * will be <tt>true</tt>, but these are not absolute requirements.
  124. * While it is typically the case that:
  125. * <blockquote>
  126. * <pre>
  127. * x.clone().equals(x)</pre></blockquote>
  128. * will be <tt>true</tt>, this is not an absolute requirement.
  129. * Copying an object will typically entail creating a new instance of
  130. * its class, but it also may require copying of internal data
  131. * structures as well. No constructors are called.
  132. * <p>
  133. * The method <tt>clone</tt> for class <tt>Object</tt> performs a
  134. * specific cloning operation. First, if the class of this object does
  135. * not implement the interface <tt>Cloneable</tt>, then a
  136. * <tt>CloneNotSupportedException</tt> is thrown. Note that all arrays
  137. * are considered to implement the interface <tt>Cloneable</tT>.
  138. * Otherwise, this method creates a new instance of the class of this
  139. * object and initializes all its fields with exactly the contents of
  140. * the corresponding fields of this object, as if by assignment; the
  141. * contents of the fields are not themselves cloned. Thus, this method
  142. * performs a "shallow copy" of this object, not a "deep copy" operation.
  143. * <p>
  144. * The class <tt>Object</tt> does not itself implement the interface
  145. * <tt>Cloneable</tt>, so calling the <tt>clone</tt> method on an object
  146. * whose class is <tt>Object</tt> will result in throwing an
  147. * exception at run time. The <tt>clone</tt> method is implemented by
  148. * the class <tt>Object</tt> as a convenient, general utility for
  149. * subclasses that implement the interface <tt>Cloneable</tt>, possibly
  150. * also overriding the <tt>clone</tt> method, in which case the
  151. * overriding definition can refer to this utility definition by the
  152. * call:
  153. * <blockquote>
  154. * <pre>
  155. * super.clone()</pre></blockquote>
  156. *
  157. * @return a clone of this instance.
  158. * @exception CloneNotSupportedException if the object's class does not
  159. * support the <code>Cloneable</code> interface. Subclasses
  160. * that override the <code>clone</code> method can also
  161. * throw this exception to indicate that an instance cannot
  162. * be cloned.
  163. * @exception OutOfMemoryError if there is not enough memory.
  164. * @see java.lang.Cloneable
  165. */
  166. protected native Object clone() throws CloneNotSupportedException;
  167. /**
  168. * Returns a string representation of the object. In general, the
  169. * <code>toString</code> method returns a string that
  170. * "textually represents" this object. The result should
  171. * be a concise but informative representation that is easy for a
  172. * person to read.
  173. * It is recommended that all subclasses override this method.
  174. * <p>
  175. * The <code>toString</code> method for class <code>Object</code>
  176. * returns a string consisting of the name of the class of which the
  177. * object is an instance, the at-sign character `<code>@</code>', and
  178. * the unsigned hexadecimal representation of the hash code of the
  179. * object. In other words, this method returns a string equal to the
  180. * value of:
  181. * <blockquote>
  182. * <pre>
  183. * getClass().getName() + '@' + Integer.toHexString(hashCode())
  184. * </pre></blockquote>
  185. *
  186. * @return a string representation of the object.
  187. */
  188. public String toString() {
  189. return getClass().getName() + "@" + Integer.toHexString(hashCode());
  190. }
  191. /**
  192. * Wakes up a single thread that is waiting on this object's
  193. * monitor. If any threads are waiting on this object, one of them
  194. * is chosen to be awakened. The choice is arbitrary and occurs at
  195. * the discretion of the implementation. A thread waits on an object's
  196. * monitor by calling one of the <code>wait</code> methods.
  197. * <p>
  198. * The awakened thread will not be able to proceed until the current
  199. * thread relinquishes the lock on this object. The awakened thread will
  200. * compete in the usual manner with any other threads that might be
  201. * actively competing to synchronize on this object; for example, the
  202. * awakened thread enjoys no reliable privilege or disadvantage in being
  203. * the next thread to lock this object.
  204. * <p>
  205. * This method should only be called by a thread that is the owner
  206. * of this object's monitor. A thread becomes the owner of the
  207. * object's monitor in one of three ways:
  208. * <ul>
  209. * <li>By executing a synchronized instance method of that object.
  210. * <li>By executing the body of a <code>synchronized</code> statement
  211. * that synchronizes on the object.
  212. * <li>For objects of type <code>Class,</code> by executing a
  213. * synchronized static method of that class.
  214. * </ul>
  215. * <p>
  216. * Only one thread at a time can own an object's monitor.
  217. *
  218. * @exception IllegalMonitorStateException if the current thread is not
  219. * the owner of this object's monitor.
  220. * @see java.lang.Object#notifyAll()
  221. * @see java.lang.Object#wait()
  222. */
  223. public final native void notify();
  224. /**
  225. * Wakes up all threads that are waiting on this object's monitor. A
  226. * thread waits on an object's monitor by calling one of the
  227. * <code>wait</code> methods.
  228. * <p>
  229. * The awakened threads will not be able to proceed until the current
  230. * thread relinquishes the lock on this object. The awakened threads
  231. * will compete in the usual manner with any other threads that might
  232. * be actively competing to synchronize on this object; for example,
  233. * the awakened threads enjoy no reliable privilege or disadvantage in
  234. * being the next thread to lock this object.
  235. * <p>
  236. * This method should only be called by a thread that is the owner
  237. * of this object's monitor. See the <code>notify</code> method for a
  238. * description of the ways in which a thread can become the owner of
  239. * a monitor.
  240. *
  241. * @exception IllegalMonitorStateException if the current thread is not
  242. * the owner of this object's monitor.
  243. * @see java.lang.Object#notify()
  244. * @see java.lang.Object#wait()
  245. */
  246. public final native void notifyAll();
  247. /**
  248. * Causes current thread to wait until either another thread invokes the
  249. * {@link java.lang.Object#notify()} method or the
  250. * {@link java.lang.Object#notifyAll()} method for this object, or a
  251. * specified amount of time has elapsed.
  252. * <p>
  253. * The current thread must own this object's monitor.
  254. * <p>
  255. * This method causes the current thread (call it <var>T</var>) to
  256. * place itself in the wait set for this object and then to relinquish
  257. * any and all synchronization claims on this object. Thread <var>T</var>
  258. * becomes disabled for thread scheduling purposes and lies dormant
  259. * until one of four things happens:
  260. * <ul>
  261. * <li>Some other thread invokes the <tt>notify</tt> method for this
  262. * object and thread <var>T</var> happens to be arbitrarily chosen as
  263. * the thread to be awakened.
  264. * <li>Some other thread invokes the <tt>notifyAll</tt> method for this
  265. * object.
  266. * <li>Some other thread {@link java.lang.Thread#interrupt() interrupts}
  267. * thread <var>T</var>.
  268. * <li>The specified amount of real time has elapsed, more or less. If
  269. * <tt>timeout</tt> is zero, however, then real time is not taken into
  270. * consideration and the thread simply waits until notified.
  271. * </ul>
  272. * The thread <var>T</var> is then removed from the wait set for this
  273. * object and re-enabled for thread scheduling. It then competes in the
  274. * usual manner with other threads for the right to synchronize on the
  275. * object; once it has gained control of the object, all its
  276. * synchronization claims on the object are restored to the status quo
  277. * ante - that is, to the situation as of the time that the <tt>wait</tt>
  278. * method was invoked. Thread <var>T</var> then returns from the
  279. * invocation of the <tt>wait</tt> method. Thus, on return from the
  280. * <tt>wait</tt> method, the synchronization state of the object and of
  281. * thread <tt>T</tt> is exactly as it was when the <tt>wait</tt> method
  282. * was invoked.
  283. * <p>
  284. * If the current thread is
  285. * {@link java.lang.Thread#interrupt() interrupted} by another thread
  286. * while it is waiting, then an <tt>InterruptedException</tt> is thrown.
  287. * This exception is not thrown until the lock status of this object has
  288. * been restored as described above.
  289. * <p>
  290. * Note that the <tt>wait</tt> method, as it places the current thread
  291. * into the wait set for this object, unlocks only this object; any
  292. * other objects on which the current thread may be synchronized remain
  293. * locked while the thread waits.
  294. * <p>
  295. * This method should only be called by a thread that is the owner
  296. * of this object's monitor. See the <code>notify</code> method for a
  297. * description of the ways in which a thread can become the owner of
  298. * a monitor.
  299. *
  300. * @param timeout the maximum time to wait in milliseconds.
  301. * @exception IllegalArgumentException if the value of timeout is
  302. * negative.
  303. * @exception IllegalMonitorStateException if the current thread is not
  304. * the owner of the object's monitor.
  305. * @exception InterruptedException if another thread has interrupted
  306. * the current thread. The <i>interrupted status</i> of the
  307. * current thread is cleared when this exception is thrown.
  308. * @see java.lang.Object#notify()
  309. * @see java.lang.Object#notifyAll()
  310. */
  311. public final native void wait(long timeout) throws InterruptedException;
  312. /**
  313. * Causes current thread to wait until another thread invokes the
  314. * {@link java.lang.Object#notify()} method or the
  315. * {@link java.lang.Object#notifyAll()} method for this object, or
  316. * some other thread interrupts the current thread, or a certain
  317. * amount of real time has elapsed.
  318. * <p>
  319. * This method is similar to the <code>wait</code> method of one
  320. * argument, but it allows finer control over the amount of time to
  321. * wait for a notification before giving up. The amount of real time,
  322. * measured in nanoseconds, is given by:
  323. * <blockquote>
  324. * <pre>
  325. * 1000000*millis+nanos</pre></blockquote>
  326. * <p>
  327. * In all other respects, this method does the same thing as the
  328. * method {@link #wait(long)} of one argument. In particular,
  329. * <tt>wait(0, 0)</tt> means the same thing as <tt>wait(0)</tt>.
  330. * <p>
  331. * The current thread must own this object's monitor. The thread
  332. * releases ownership of this monitor and waits until either of the
  333. * following two conditions has occurred:
  334. * <ul>
  335. * <li>Another thread notifies threads waiting on this object's monitor
  336. * to wake up either through a call to the <code>notify</code> method
  337. * or the <code>notifyAll</code> method.
  338. * <li>The timeout period, specified by <code>timeout</code>
  339. * milliseconds plus <code>nanos</code> nanoseconds arguments, has
  340. * elapsed.
  341. * </ul>
  342. * <p>
  343. * The thread then waits until it can re-obtain ownership of the
  344. * monitor and resumes execution
  345. * <p>
  346. * This method should only be called by a thread that is the owner
  347. * of this object's monitor. See the <code>notify</code> method for a
  348. * description of the ways in which a thread can become the owner of
  349. * a monitor.
  350. *
  351. * @param timeout the maximum time to wait in milliseconds.
  352. * @param nanos additional time, in nanoseconds range
  353. * 0-999999.
  354. * @exception IllegalArgumentException if the value of timeout is
  355. * negative or the value of nanos is
  356. * not in the range 0-999999.
  357. * @exception IllegalMonitorStateException if the current thread is not
  358. * the owner of this object's monitor.
  359. * @exception InterruptedException if another thread has interrupted
  360. * the current thread. The <i>interrupted status</i> of the
  361. * current thread is cleared when this exception is thrown.
  362. */
  363. public final void wait(long timeout, int nanos) throws InterruptedException {
  364. if (timeout < 0) {
  365. throw new IllegalArgumentException("timeout value is negative");
  366. }
  367. if (nanos < 0 || nanos > 999999) {
  368. throw new IllegalArgumentException(
  369. "nanosecond timeout value out of range");
  370. }
  371. if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
  372. timeout++;
  373. }
  374. wait(timeout);
  375. }
  376. /**
  377. * Causes current thread to wait until another thread invokes the
  378. * {@link java.lang.Object#notify()} method or the
  379. * {@link java.lang.Object#notifyAll()} method for this object.
  380. * In other word's this method behaves exactly as if it simply
  381. * performs the call <tt>wait(0)</tt>.
  382. * <p>
  383. * The current thread must own this object's monitor. The thread
  384. * releases ownership of this monitor and waits until another thread
  385. * notifies threads waiting on this object's monitor to wake up
  386. * either through a call to the <code>notify</code> method or the
  387. * <code>notifyAll</code> method. The thread then waits until it can
  388. * re-obtain ownership of the monitor and resumes execution.
  389. * <p>
  390. * This method should only be called by a thread that is the owner
  391. * of this object's monitor. See the <code>notify</code> method for a
  392. * description of the ways in which a thread can become the owner of
  393. * a monitor.
  394. *
  395. * @exception IllegalMonitorStateException if the current thread is not
  396. * the owner of the object's monitor.
  397. * @exception InterruptedException if another thread has interrupted
  398. * the current thread. The <i>interrupted status</i> of the
  399. * current thread is cleared when this exception is thrown.
  400. * @see java.lang.Object#notify()
  401. * @see java.lang.Object#notifyAll()
  402. */
  403. public final void wait() throws InterruptedException {
  404. wait(0);
  405. }
  406. /**
  407. * Called by the garbage collector on an object when garbage collection
  408. * determines that there are no more references to the object.
  409. * A subclass overrides the <code>finalize</code> method to dispose of
  410. * system resources or to perform other cleanup.
  411. * <p>
  412. * The general contract of <tt>finalize</tt> is that it is invoked
  413. * if and when the Java<font size="-2"><sup>TM</sup></font> virtual
  414. * machine has determined that there is no longer any
  415. * means by which this object can be accessed by any thread that has
  416. * not yet died, except as a result of an action taken by the
  417. * finalization of some other object or class which is ready to be
  418. * finalized. The <tt>finalize</tt> method may take any action, including
  419. * making this object available again to other threads; the usual purpose
  420. * of <tt>finalize</tt>, however, is to perform cleanup actions before
  421. * the object is irrevocably discarded. For example, the finalize method
  422. * for an object that represents an input/output connection might perform
  423. * explicit I/O transactions to break the connection before the object is
  424. * permanently discarded.
  425. * <p>
  426. * The <tt>finalize</tt> method of class <tt>Object</tt> performs no
  427. * special action; it simply returns normally. Subclasses of
  428. * <tt>Object</tt> may override this definition.
  429. * <p>
  430. * The Java programming language does not guarantee which thread will
  431. * invoke the <tt>finalize</tt> method for any given object. It is
  432. * guaranteed, however, that the thread that invokes finalize will not
  433. * be holding any user-visible synchronization locks when finalize is
  434. * invoked. If an uncaught exception is thrown by the finalize method,
  435. * the exception is ignored and finalization of that object terminates.
  436. * <p>
  437. * After the <tt>finalize</tt> method has been invoked for an object, no
  438. * further action is taken until the Java virtual machine has again
  439. * determined that there is no longer any means by which this object can
  440. * be accessed by any thread that has not yet died, including possible
  441. * actions by other objects or classes which are ready to be finalized,
  442. * at which point the object may be discarded.
  443. * <p>
  444. * The <tt>finalize</tt> method is never invoked more than once by a Java
  445. * virtual machine for any given object.
  446. * <p>
  447. * Any exception thrown by the <code>finalize</code> method causes
  448. * the finalization of this object to be halted, but is otherwise
  449. * ignored.
  450. *
  451. * @throws Throwable the <code>Exception</code> raised by this method
  452. */
  453. protected void finalize() throws Throwable { }
  454. }