< prev index next >

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

Print this page

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



 30 
 31 /**
 32  * Class {@code Object} is the root of the class hierarchy.
 33  * Every class has {@code Object} as a superclass. All objects,
 34  * including arrays, implement the methods of this class.







 35  *
 36  * @see     java.lang.Class
 37  * @since   1.0
 38  */
 39 public class Object {
 40 
 41     /**
 42      * Constructs a new object.
 43      */
 44     @IntrinsicCandidate
 45     public Object() {}
 46 
 47     /**
 48      * Returns the runtime class of this {@code Object}. The returned
 49      * {@code Class} object is the object that is locked by {@code
 50      * static synchronized} methods of the represented class.
 51      *
 52      * <p><b>The actual result type is {@code Class<? extends |X|>}
 53      * where {@code |X|} is the erasure of the static type of the
 54      * expression on which {@code getClass} is called.</b> For

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

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






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

 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.misc.Blocker;
 29 import jdk.internal.vm.annotation.IntrinsicCandidate;
 30 import jdk.internal.access.SharedSecrets;
 31 
 32 import java.util.Objects;
 33 
 34 /**
 35  * Class {@code Object} is the root of the class hierarchy.
 36  * Every class has {@code Object} as a superclass. All objects,
 37  * including arrays, implement the methods of this class.
 38  * <p>
 39  * Subclasses of {@code java.lang.Object} can be either {@linkplain Class#isIdentity() identity classes}
 40  * or {@linkplain Class#isValue value classes}.
 41  * The class {@code Object} itself is neither an identity class nor a value class.
 42  * See {@jls The Java Language Specification  8.1.1.5 identity and value Classes}.
 43  * An instance can be created with {@code new Object()}, those instances are
 44  * {@link Objects#isIdentityObject(Object) an identity object}.
 45  *
 46  * @see     java.lang.Class
 47  * @since   1.0
 48  */
 49 public class Object {
 50 
 51     /**
 52      * Constructs a new object.
 53      */
 54     @IntrinsicCandidate
 55     public Object() {}
 56 
 57     /**
 58      * Returns the runtime class of this {@code Object}. The returned
 59      * {@code Class} object is the object that is locked by {@code
 60      * static synchronized} methods of the represented class.
 61      *
 62      * <p><b>The actual result type is {@code Class<? extends |X|>}
 63      * where {@code |X|} is the erasure of the static type of the
 64      * expression on which {@code getClass} is called.</b> For

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