src/java.base/share/classes/java/lang/Object.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8076112 Sdiff src/java.base/share/classes/java/lang

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

Print this page




   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      * Constructs a new object.
  46      */

  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
  52      * static synchronized} methods of the represented class.
  53      *
  54      * <p><b>The actual result type is {@code Class<? extends |X|>}
  55      * where {@code |X|} is the erasure of the static type of the
  56      * expression on which {@code getClass} is called.</b> For
  57      * example, no cast is required in this code fragment:</p>
  58      *
  59      * <p>
  60      * {@code Number n = 0;                             }<br>
  61      * {@code Class<? extends Number> c = n.getClass(); }
  62      * </p>
  63      *
  64      * @return The {@code Class} object that represents the runtime
  65      *         class of this object.
  66      * @jls 15.8.2 Class Literals
  67      */

  68     public final native Class<?> getClass();
  69 
  70     /**
  71      * Returns a hash code value for the object. This method is
  72      * supported for the benefit of hash tables such as those provided by
  73      * {@link java.util.HashMap}.
  74      * <p>
  75      * The general contract of {@code hashCode} is:
  76      * <ul>
  77      * <li>Whenever it is invoked on the same object more than once during
  78      *     an execution of a Java application, the {@code hashCode} method
  79      *     must consistently return the same integer, provided no information
  80      *     used in {@code equals} comparisons on the object is modified.
  81      *     This integer need not remain consistent from one execution of an
  82      *     application to another execution of the same application.
  83      * <li>If two objects are equal according to the {@code equals(Object)}
  84      *     method, then calling the {@code hashCode} method on each of
  85      *     the two objects must produce the same integer result.
  86      * <li>It is <em>not</em> required that if two objects are unequal
  87      *     according to the {@link java.lang.Object#equals(java.lang.Object)}
  88      *     method, then calling the {@code hashCode} method on each of the
  89      *     two objects must produce distinct integer results.  However, the
  90      *     programmer should be aware that producing distinct integer results
  91      *     for unequal objects may improve the performance of hash tables.
  92      * </ul>
  93      * <p>
  94      * As much as is reasonably practical, the hashCode method defined
  95      * by class {@code Object} does return distinct integers for
  96      * distinct objects. (The hashCode may or may not be implemented
  97      * as some function of an object's memory address at some point
  98      * in time.)
  99      *
 100      * @return  a hash code value for this object.
 101      * @see     java.lang.Object#equals(java.lang.Object)
 102      * @see     java.lang.System#identityHashCode
 103      */

 104     public native int hashCode();
 105 
 106     /**
 107      * Indicates whether some other object is "equal to" this one.
 108      * <p>
 109      * The {@code equals} method implements an equivalence relation
 110      * on non-null object references:
 111      * <ul>
 112      * <li>It is <i>reflexive</i>: for any non-null reference value
 113      *     {@code x}, {@code x.equals(x)} should return
 114      *     {@code true}.
 115      * <li>It is <i>symmetric</i>: for any non-null reference values
 116      *     {@code x} and {@code y}, {@code x.equals(y)}
 117      *     should return {@code true} if and only if
 118      *     {@code y.equals(x)} returns {@code true}.
 119      * <li>It is <i>transitive</i>: for any non-null reference values
 120      *     {@code x}, {@code y}, and {@code z}, if
 121      *     {@code x.equals(y)} returns {@code true} and
 122      *     {@code y.equals(z)} returns {@code true}, then
 123      *     {@code x.equals(z)} should return {@code true}.


 196      * is {@code T[]} where T is any reference or primitive type.
 197      * Otherwise, this method creates a new instance of the class of this
 198      * object and initializes all its fields with exactly the contents of
 199      * the corresponding fields of this object, as if by assignment; the
 200      * contents of the fields are not themselves cloned. Thus, this method
 201      * performs a "shallow copy" of this object, not a "deep copy" operation.
 202      * <p>
 203      * The class {@code Object} does not itself implement the interface
 204      * {@code Cloneable}, so calling the {@code clone} method on an object
 205      * whose class is {@code Object} will result in throwing an
 206      * exception at run time.
 207      *
 208      * @return     a clone of this instance.
 209      * @throws  CloneNotSupportedException  if the object's class does not
 210      *               support the {@code Cloneable} interface. Subclasses
 211      *               that override the {@code clone} method can also
 212      *               throw this exception to indicate that an instance cannot
 213      *               be cloned.
 214      * @see java.lang.Cloneable
 215      */

 216     protected native Object clone() throws CloneNotSupportedException;
 217 
 218     /**
 219      * Returns a string representation of the object. In general, the
 220      * {@code toString} method returns a string that
 221      * "textually represents" this object. The result should
 222      * be a concise but informative representation that is easy for a
 223      * person to read.
 224      * It is recommended that all subclasses override this method.
 225      * <p>
 226      * The {@code toString} method for class {@code Object}
 227      * returns a string consisting of the name of the class of which the
 228      * object is an instance, the at-sign character `{@code @}', and
 229      * the unsigned hexadecimal representation of the hash code of the
 230      * object. In other words, this method returns a string equal to the
 231      * value of:
 232      * <blockquote>
 233      * <pre>
 234      * getClass().getName() + '@' + Integer.toHexString(hashCode())
 235      * </pre></blockquote>




   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.HotSpotIntrinsicCandidate;
  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  * @author  unascribed
  36  * @see     java.lang.Class
  37  * @since   1.0
  38  */
  39 public class Object {
  40 
  41     private static native void registerNatives();
  42     static {
  43         registerNatives();
  44     }
  45 
  46     /**
  47      * Constructs a new object.
  48      */
  49     @HotSpotIntrinsicCandidate
  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
  68      *         class of this object.
  69      * @jls 15.8.2 Class Literals
  70      */
  71     @HotSpotIntrinsicCandidate
  72     public final native Class<?> getClass();
  73 
  74     /**
  75      * Returns a hash code value for the object. This method is
  76      * supported for the benefit of hash tables such as those provided by
  77      * {@link java.util.HashMap}.
  78      * <p>
  79      * The general contract of {@code hashCode} is:
  80      * <ul>
  81      * <li>Whenever it is invoked on the same object more than once during
  82      *     an execution of a Java application, the {@code hashCode} method
  83      *     must consistently return the same integer, provided no information
  84      *     used in {@code equals} comparisons on the object is modified.
  85      *     This integer need not remain consistent from one execution of an
  86      *     application to another execution of the same application.
  87      * <li>If two objects are equal according to the {@code equals(Object)}
  88      *     method, then calling the {@code hashCode} method on each of
  89      *     the two objects must produce the same integer result.
  90      * <li>It is <em>not</em> required that if two objects are unequal
  91      *     according to the {@link java.lang.Object#equals(java.lang.Object)}
  92      *     method, then calling the {@code hashCode} method on each of the
  93      *     two objects must produce distinct integer results.  However, the
  94      *     programmer should be aware that producing distinct integer results
  95      *     for unequal objects may improve the performance of hash tables.
  96      * </ul>
  97      * <p>
  98      * As much as is reasonably practical, the hashCode method defined
  99      * by class {@code Object} does return distinct integers for
 100      * distinct objects. (The hashCode may or may not be implemented
 101      * as some function of an object's memory address at some point
 102      * in time.)
 103      *
 104      * @return  a hash code value for this object.
 105      * @see     java.lang.Object#equals(java.lang.Object)
 106      * @see     java.lang.System#identityHashCode
 107      */
 108     @HotSpotIntrinsicCandidate
 109     public native int hashCode();
 110 
 111     /**
 112      * Indicates whether some other object is "equal to" this one.
 113      * <p>
 114      * The {@code equals} method implements an equivalence relation
 115      * on non-null object references:
 116      * <ul>
 117      * <li>It is <i>reflexive</i>: for any non-null reference value
 118      *     {@code x}, {@code x.equals(x)} should return
 119      *     {@code true}.
 120      * <li>It is <i>symmetric</i>: for any non-null reference values
 121      *     {@code x} and {@code y}, {@code x.equals(y)}
 122      *     should return {@code true} if and only if
 123      *     {@code y.equals(x)} returns {@code true}.
 124      * <li>It is <i>transitive</i>: for any non-null reference values
 125      *     {@code x}, {@code y}, and {@code z}, if
 126      *     {@code x.equals(y)} returns {@code true} and
 127      *     {@code y.equals(z)} returns {@code true}, then
 128      *     {@code x.equals(z)} should return {@code true}.


 201      * is {@code T[]} where T is any reference or primitive type.
 202      * Otherwise, this method creates a new instance of the class of this
 203      * object and initializes all its fields with exactly the contents of
 204      * the corresponding fields of this object, as if by assignment; the
 205      * contents of the fields are not themselves cloned. Thus, this method
 206      * performs a "shallow copy" of this object, not a "deep copy" operation.
 207      * <p>
 208      * The class {@code Object} does not itself implement the interface
 209      * {@code Cloneable}, so calling the {@code clone} method on an object
 210      * whose class is {@code Object} will result in throwing an
 211      * exception at run time.
 212      *
 213      * @return     a clone of this instance.
 214      * @throws  CloneNotSupportedException  if the object's class does not
 215      *               support the {@code Cloneable} interface. Subclasses
 216      *               that override the {@code clone} method can also
 217      *               throw this exception to indicate that an instance cannot
 218      *               be cloned.
 219      * @see java.lang.Cloneable
 220      */
 221     @HotSpotIntrinsicCandidate
 222     protected native Object clone() throws CloneNotSupportedException;
 223 
 224     /**
 225      * Returns a string representation of the object. In general, the
 226      * {@code toString} method returns a string that
 227      * "textually represents" this object. The result should
 228      * be a concise but informative representation that is easy for a
 229      * person to read.
 230      * It is recommended that all subclasses override this method.
 231      * <p>
 232      * The {@code toString} method for class {@code Object}
 233      * returns a string consisting of the name of the class of which the
 234      * object is an instance, the at-sign character `{@code @}', and
 235      * the unsigned hexadecimal representation of the hash code of the
 236      * object. In other words, this method returns a string equal to the
 237      * value of:
 238      * <blockquote>
 239      * <pre>
 240      * getClass().getName() + '@' + Integer.toHexString(hashCode())
 241      * </pre></blockquote>


src/java.base/share/classes/java/lang/Object.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File