< prev index next >

src/java.base/share/classes/java/lang/Object.java

Print this page

  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 import jdk.internal.vm.annotation.IntrinsicCandidate;



 29 
 30 /**
 31  * Class {@code Object} is the root of the class hierarchy.
 32  * Every class has {@code Object} as a superclass. All objects,
 33  * including arrays, implement the methods of this class.
 34  *
 35  * @see     java.lang.Class
 36  * @since   1.0
 37  */
 38 public class Object {
 39 
 40     /**
 41      * Constructs a new object.



 42      */
 43     @IntrinsicCandidate
 44     public Object() {}
 45 
 46     /**
 47      * Returns the runtime class of this {@code Object}. The returned
 48      * {@code Class} object is the object that is locked by {@code
 49      * static synchronized} methods of the represented class.
 50      *
 51      * <p><b>The actual result type is {@code Class<? extends |X|>}
 52      * where {@code |X|} is the erasure of the static type of the
 53      * expression on which {@code getClass} is called.</b> For
 54      * example, no cast is required in this code fragment:</p>
 55      *
 56      * <p>
 57      * {@code Number n = 0;                             }<br>
 58      * {@code Class<? extends Number> c = n.getClass(); }
 59      * </p>
 60      *
 61      * @return The {@code Class} object that represents the runtime

222      *               that override the {@code clone} method can also
223      *               throw this exception to indicate that an instance cannot
224      *               be cloned.
225      * @see java.lang.Cloneable
226      */
227     @IntrinsicCandidate
228     protected native Object clone() throws CloneNotSupportedException;
229 
230     /**
231      * Returns a string representation of the object.
232      * @apiNote
233      * In general, the
234      * {@code toString} method returns a string that
235      * "textually represents" this object. The result should
236      * be a concise but informative representation that is easy for a
237      * person to read.
238      * It is recommended that all subclasses override this method.
239      * The string output is not necessarily stable over time or across
240      * JVM invocations.
241      * @implSpec
242      * The {@code toString} method for class {@code Object}

243      * returns a string consisting of the name of the class of which the
244      * object is an instance, the at-sign character `{@code @}', and
245      * the unsigned hexadecimal representation of the hash code of the
246      * object. In other words, this method returns a string equal to the
247      * value of:
248      * <blockquote>
249      * <pre>
250      * getClass().getName() + '@' + Integer.toHexString(hashCode())
251      * </pre></blockquote>






252      *
253      * @return  a string representation of the object.
254      */
255     public String toString() {
256         return getClass().getName() + "@" + Integer.toHexString(hashCode());
257     }
258 
259     /**
260      * Wakes up a single thread that is waiting on this object's
261      * monitor. If any threads are waiting on this object, one of them
262      * is chosen to be awakened. The choice is arbitrary and occurs at
263      * the discretion of the implementation. A thread waits on an object's
264      * monitor by calling one of the {@code wait} methods.
265      * <p>
266      * The awakened thread will not be able to proceed until the current
267      * thread relinquishes the lock on this object. The awakened thread will
268      * compete in the usual manner with any other threads that might be
269      * actively competing to synchronize on this object; for example, the
270      * awakened thread enjoys no reliable privilege or disadvantage in being
271      * the next thread to lock this object.

  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 import jdk.internal.vm.annotation.IntrinsicCandidate;
 29 import jdk.internal.access.SharedSecrets;
 30 
 31 import java.util.Objects;
 32 
 33 /**
 34  * Class {@code Object} is the root of the class hierarchy.
 35  * Every class has {@code Object} as a superclass. All objects,
 36  * including arrays, implement the methods of this class.
 37  *
 38  * @see     java.lang.Class
 39  * @since   1.0
 40  */
 41 public class Object {
 42 
 43     /**
 44      * Constructs a new object.
 45      *
 46      * @apiNote {@link Objects#newIdentity java.util.Objects.newIdentity()}
 47      * should be used instead of {@code new Object()}.
 48      */
 49     @IntrinsicCandidate
 50     public Object() {}
 51 
 52     /**
 53      * Returns the runtime class of this {@code Object}. The returned
 54      * {@code Class} object is the object that is locked by {@code
 55      * static synchronized} methods of the represented class.
 56      *
 57      * <p><b>The actual result type is {@code Class<? extends |X|>}
 58      * where {@code |X|} is the erasure of the static type of the
 59      * expression on which {@code getClass} is called.</b> For
 60      * example, no cast is required in this code fragment:</p>
 61      *
 62      * <p>
 63      * {@code Number n = 0;                             }<br>
 64      * {@code Class<? extends Number> c = n.getClass(); }
 65      * </p>
 66      *
 67      * @return The {@code Class} object that represents the runtime

228      *               that override the {@code clone} method can also
229      *               throw this exception to indicate that an instance cannot
230      *               be cloned.
231      * @see java.lang.Cloneable
232      */
233     @IntrinsicCandidate
234     protected native Object clone() throws CloneNotSupportedException;
235 
236     /**
237      * Returns a string representation of the object.
238      * @apiNote
239      * In general, the
240      * {@code toString} method returns a string that
241      * "textually represents" this object. The result should
242      * be a concise but informative representation that is easy for a
243      * person to read.
244      * It is recommended that all subclasses override this method.
245      * The string output is not necessarily stable over time or across
246      * JVM invocations.
247      * @implSpec
248      * If this object is an instance of an identity class, then
249      * the {@code toString} method for class {@code Object}
250      * returns a string consisting of the name of the class of which the
251      * object is an instance, the at-sign character `{@code @}', and
252      * the unsigned hexadecimal representation of the hash code of the
253      * object. In other words, this method returns a string equal to the
254      * value of:
255      * <blockquote>
256      * <pre>
257      * getClass().getName() + '@' + Integer.toHexString(hashCode())
258      * </pre></blockquote>
259      * <p>
260      * If this object is an instance of a primitive class, then
261      * the {@code toString} method returns a string which contains
262      * the name of the primitive class, and string representations of
263      * all its fields.  The precise format produced by this method
264      * is unspecified and subject to change.
265      *
266      * @return  a string representation of the object.
267      */
268     public String toString() {
269         return getClass().getName() + "@" + Integer.toHexString(hashCode());
270     }
271 
272     /**
273      * Wakes up a single thread that is waiting on this object's
274      * monitor. If any threads are waiting on this object, one of them
275      * is chosen to be awakened. The choice is arbitrary and occurs at
276      * the discretion of the implementation. A thread waits on an object's
277      * monitor by calling one of the {@code wait} methods.
278      * <p>
279      * The awakened thread will not be able to proceed until the current
280      * thread relinquishes the lock on this object. The awakened thread will
281      * compete in the usual manner with any other threads that might be
282      * actively competing to synchronize on this object; for example, the
283      * awakened thread enjoys no reliable privilege or disadvantage in being
284      * the next thread to lock this object.
< prev index next >