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