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