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