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