< prev index next >

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

Print this page




  75  * then it must be the case that {@code r.equals(copy)}.
  76  *
  77  * @apiNote
  78  * A record class that {@code implements} {@link java.io.Serializable} is said
  79  * to be a <i>serializable record</i>. Serializable records are serialized and
  80  * deserialized differently than ordinary serializable objects. During
  81  * deserialization the record's canonical constructor is invoked to construct
  82  * the record object. Certain serialization-related methods, such as readObject
  83  * and writeObject, are ignored for serializable records. More information about
  84  * serializable records can be found in
  85  * <a href="{@docRoot}/java.base/java/io/ObjectInputStream.html#record-serialization">record serialization</a>.
  86  *
  87  * @jls 8.10 Record Types
  88  * @since 14
  89  */
  90 @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.RECORDS,
  91                              essentialAPI=true)
  92 public abstract class Record {
  93     /**
  94      * Indicates whether some other object is "equal to" this one.  In addition
  95      * to the general contract of {@link Object#equals(Object)},
  96      * record classes must further participate in the invariant that when
  97      * a record instance is "copied" by passing the result of the record component
  98      * accessor methods to the canonical constructor, as follows:
  99      * <pre>
 100      *     R copy = new R(r.c1(), r.c2(), ..., r.cn());
 101      * </pre>
 102      * then it must be the case that {@code r.equals(copy)}.
 103      *
 104      * @implSpec
 105      * The implicitly provided implementation returns {@code true} if and
 106      * only if the argument is an instance of the same record type as this object,
 107      * and each component of this record is equal to the corresponding component
 108      * of the argument, according to {@link java.util.Objects#equals(Object,Object)}
 109      * for components whose types are reference types, and according to the semantics
 110      * of the {@code equals} method on the corresponding primitive wrapper type.




















 111      *
 112      * @see java.util.Objects#equals(Object,Object)
 113      *
 114      * @param   obj   the reference object with which to compare.
 115      * @return  {@code true} if this object is the same as the obj
 116      *          argument; {@code false} otherwise.
 117      */
 118     @Override
 119     public abstract boolean equals(Object obj);
 120 
 121     /**
 122      * Obeys the general contract of {@link Object#hashCode Object.hashCode}.
 123      *
 124      * @implSpec
 125      * The implicitly provided implementation returns a hash code value derived
 126      * by combining the hash code value for all the components, according to
 127      * {@link Object#hashCode()} for components whose types are reference types,
 128      * or the primitive wrapper hash code for components whose types are primitive
 129      * types.
 130      *
 131      * @see     Object#hashCode()
 132      *
 133      * @return  a hash code value for this object.
 134      */
 135     @Override


  75  * then it must be the case that {@code r.equals(copy)}.
  76  *
  77  * @apiNote
  78  * A record class that {@code implements} {@link java.io.Serializable} is said
  79  * to be a <i>serializable record</i>. Serializable records are serialized and
  80  * deserialized differently than ordinary serializable objects. During
  81  * deserialization the record's canonical constructor is invoked to construct
  82  * the record object. Certain serialization-related methods, such as readObject
  83  * and writeObject, are ignored for serializable records. More information about
  84  * serializable records can be found in
  85  * <a href="{@docRoot}/java.base/java/io/ObjectInputStream.html#record-serialization">record serialization</a>.
  86  *
  87  * @jls 8.10 Record Types
  88  * @since 14
  89  */
  90 @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.RECORDS,
  91                              essentialAPI=true)
  92 public abstract class Record {
  93     /**
  94      * Indicates whether some other object is "equal to" this one.  In addition
  95      * to the general contract of {@link Object#equals(Object) Object.equals},
  96      * record classes must further obey the invariant that when
  97      * a record instance is "copied" by passing the result of the record component
  98      * accessor methods to the canonical constructor, as follows:
  99      * <pre>
 100      *     R copy = new R(r.c1(), r.c2(), ..., r.cn());
 101      * </pre>
 102      * then it must be the case that {@code r.equals(copy)}.
 103      *
 104      * @implSpec
 105      * The implicitly provided implementation returns {@code true} if
 106      * and only if the argument is an instance of the same record type
 107      * as this object, and each component of this record is equal to
 108      * the corresponding component of the argument; otherwise, {@code
 109      * false} is returned. Equality of a component {@code c} is
 110      * determined as follows:
 111      * <ul>
 112      *
 113      * <li> If the component is of a reference type, the component is
 114      * considered equal if and only if {@link
 115      * java.util.Objects#equals(Object,Object)
 116      * Objects.equals(this.c(), r.c()} would return {@code true}.
 117      *
 118      * <li> If the component is of a primitive type, using the
 119      * corresponding primitive wrapper class {@code PW} (the wrapper
 120      * class {@code java.lang.Integer} for {@code int}, and so on) the
 121      * component is considered equal if and only if {@code
 122      * PW.valueOf(this.c()).equals(PW.valueOf(r.c()))} would return
 123      * {@code true}.
 124      *
 125      * </ul>
 126      *
 127      * The implicitly provided implementation conforms to the
 128      * semantics described above; the implementation may or may not
 129      * accomplish this by using calls to the particular methods
 130      * listed.
 131      *
 132      * @see java.util.Objects#equals(Object,Object)
 133      *
 134      * @param   obj   the reference object with which to compare.
 135      * @return  {@code true} if this object is equal to the
 136      *          argument; {@code false} otherwise.
 137      */
 138     @Override
 139     public abstract boolean equals(Object obj);
 140 
 141     /**
 142      * Obeys the general contract of {@link Object#hashCode Object.hashCode}.
 143      *
 144      * @implSpec
 145      * The implicitly provided implementation returns a hash code value derived
 146      * by combining the hash code value for all the components, according to
 147      * {@link Object#hashCode()} for components whose types are reference types,
 148      * or the primitive wrapper hash code for components whose types are primitive
 149      * types.
 150      *
 151      * @see     Object#hashCode()
 152      *
 153      * @return  a hash code value for this object.
 154      */
 155     @Override
< prev index next >