jdk/src/java.base/share/classes/java/lang/doc-files/ValueBased.html
author rriggs
Tue, 16 Feb 2016 11:36:20 -0500
changeset 35978 752d505da547
parent 25859 3317bb8137f4
child 46900 e92e67ed12b4
permissions -rw-r--r--
8148775: Spec for j.l.ProcessBuilder.Redirect.DISCARD need to be improved Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21966
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
     1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
     2
<html lang="en">
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
     3
<head>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
     4
  <title>Value-based Classes</title>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
     5
  <link rel="stylesheet" type="text/css" href="../../../stylesheet.css" title="Style">
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
     6
</head>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
     7
<body>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
     8
<h2 id="ValueBased">Value-based Classes</h2>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
     9
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    10
Some classes, such as <code>java.util.Optional</code> and
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    11
<code>java.time.LocalDateTime</code>, are <em>value-based</em>.  Instances of a
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    12
value-based class:
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    13
<ul>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    14
    <li>are final and immutable (though may contain references to mutable
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    15
        objects);</li>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    16
    <li>have implementations of <code>equals</code>,
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    17
        <code>hashCode</code>, and <code>toString</code> which are computed
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    18
        solely from the instance's state and not from its identity or the state
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    19
        of any other object or variable;</li>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    20
    <li>make no use of identity-sensitive operations such as reference
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    21
        equality (<code>==</code>) between instances, identity hash code of
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    22
        instances, or synchronization on an instances's intrinsic lock;</li>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    23
    <li>are considered equal solely based on <code>equals()</code>, not
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    24
        based on reference equality (<code>==</code>);</li>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    25
    <li>do not have accessible constructors, but are instead instantiated
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    26
        through factory methods which make no committment as to the identity
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    27
        of returned instances;</li>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    28
    <li>are <em>freely substitutable</em> when equal, meaning that interchanging
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    29
        any two instances <code>x</code> and <code>y</code> that are equal
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    30
        according to <code>equals()</code> in any computation or method
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    31
        invocation should produce no visible change in behavior.
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    32
    </li>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    33
</ul>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    34
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    35
<p>A program may produce unpredictable results if it attempts to distinguish two
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    36
    references to equal values of a value-based class, whether directly via reference
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    37
    equality or indirectly via an appeal to synchronization, identity hashing,
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    38
    serialization, or any other identity-sensitive mechanism.  Use of such
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    39
    identity-sensitive operations on instances of value-based classes may have
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    40
    unpredictable effects and should be avoided.</p>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    41
</body>
db858b7c6313 8028816: Add value-type notice to Optional* classes
briangoetz
parents:
diff changeset
    42
</html>