1 <!doctype html>
2 <!--
3 Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
4 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
6 This code is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License version 2 only, as
8 published by the Free Software Foundation. Oracle designates this
9 particular file as subject to the "Classpath" exception as provided
10 by Oracle in the LICENSE file that accompanied this code.
11
12 This code is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 version 2 for more details (a copy is included in the LICENSE file that
16 accompanied this code).
17
18 You should have received a copy of the GNU General Public License version
19 2 along with this work; if not, write to the Free Software Foundation,
20 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23 or visit www.oracle.com if you need additional information or have any
24 questions.
25 -->
26 <html lang="en">
27 <head>
28 <title>Value-based Classes</title>
29 </head>
30 <body>
31 <h1 id="ValueBased">{@index "Value-based Classes"}</h1>
32
33 Some classes, such as {@code java.lang.Integer} and
34 {@code java.time.LocalDate}, are <em>value-based</em>.
35 A value-based class has the following properties:
36 <ul>
37 <li>the class declares only final instance fields (though these may contain references
38 to mutable objects);</li>
39 <li>the class's implementations of {@code equals}, {@code hashCode},
40 and {@code toString} compute their results solely from the values
41 of the class's instance fields (and the members of the objects they
42 reference), not from the instance's identity;</li>
43 <li>the class's methods treat instances as <em>freely substitutable</em>
44 when equal, meaning that interchanging any two instances {@code x} and
45 {@code y} that are equal according to {@code equals()} produces no
46 visible change in the behavior of the class's methods;</li>
47 <li>the class performs no synchronization using an instance's monitor;</li>
48 <li>the class does not declare (or discourages use of) accessible constructors;</li>
49 <li>the class does not provide any instance creation mechanism that promises
50 a unique identity on each method call—in particular, any factory
51 method's contract must allow for the possibility that if two independently-produced
52 instances are equal according to {@code equals()}, they may also be
53 equal according to {@code ==};</li>
54 <li>the class is final, and extends either {@code Object} or a hierarchy of
55 abstract classes that declare no instance fields or instance initializers
56 and whose constructors are empty.</li>
57 </ul>
58
59 <p>When two instances of a value-based class are equal (according to `equals`), a program
60 should not attempt to distinguish between their identities, whether directly via reference
61 equality {@code ==} or indirectly via an appeal to synchronization, identity hashing,
62 serialization, or any other identity-sensitive mechanism.</p>
63
64 <p>Synchronization on instances of value-based classes is strongly discouraged,
65 because the programmer cannot guarantee exclusive ownership of the
66 associated monitor.</p>
67
68 <p>Identity-related behavior of value-based classes may change in a future release.
69 For example, synchronization may fail.</p>
70
71 </body>
72 </html>
|
1 <!doctype html>
2 <!--
3 Copyright (c) 2013, 2026, Oracle and/or its affiliates. All rights reserved.
4 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
6 This code is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License version 2 only, as
8 published by the Free Software Foundation. Oracle designates this
9 particular file as subject to the "Classpath" exception as provided
10 by Oracle in the LICENSE file that accompanied this code.
11
12 This code is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 version 2 for more details (a copy is included in the LICENSE file that
16 accompanied this code).
17
18 You should have received a copy of the GNU General Public License version
19 2 along with this work; if not, write to the Free Software Foundation,
20 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23 or visit www.oracle.com if you need additional information or have any
24 questions.
25 -->
26 <html lang="en">
27 <head>
28 <title>Value-based Classes</title>
29 </head>
30 <body>
31 <h1 id="ValueBased">{@index "Value-based Classes"}</h1>
32
33 Some classes, such as {@code java.lang.Integer} and
34 {@code java.time.LocalDate}, are <em>value-based</em>.
35 In a future release, they may become value classes (JLS
36 {@jls value-objects-8.1.1.5}).
37
38 <p>A value-based class has the following properties:</p>
39
40 <ul>
41 <li>all instance fields declared in the class and all of its superclasses
42 are final (though these may contain references to mutable objects);</li>
43 <li>the class's implementations of {@code equals}, {@code hashCode},
44 and {@code toString} compute their results solely from the values
45 of the class's instance fields (and the members of the objects they
46 reference), not from the instance's identity;</li>
47 <li>the class's methods treat instances as <em>freely substitutable</em>
48 when equal, meaning that interchanging any two instances {@code x} and
49 {@code y} that are equal according to {@code equals()} produces no
50 visible change in the behavior of the class's methods;</li>
51 <li>the class performs no synchronization using an instance's monitor;</li>
52 <li>the class does not declare, or discourages use of, accessible constructors
53 (because each constructor call promises a unique identity);</li>
54 <li>the class does not provide any instance creation mechanism that promises
55 a unique identity on each method call—in particular, any factory
56 method's contract must allow for the possibility that if two independently-produced
57 instances are equal according to {@code equals()}, they may also be
58 equal according to {@code ==};</li>
59 <li>the class is final, and extends either {@code Object} or a hierarchy of
60 abstract classes.</li>
61 </ul>
62
63 <p>When two instances of a value-based class are equal (according to `equals`), a program
64 should not attempt to distinguish between their identities, whether directly via reference
65 equality {@code ==} or indirectly via an appeal to synchronization, identity hashing,
66 serialization, or any other identity-sensitive mechanism.</p>
67
68 <p>Synchronization on instances of value-based classes is strongly discouraged,
69 because the programmer cannot guarantee exclusive ownership of the
70 associated monitor.</p>
71
72 <p>When preview features are enabled, some value-based classes become value classes.
73 Instances of these classes become value objects, which have different identity-related
74 behaviors compared to identity objects:</p>
75 <ul>
76 <li>Previously distinguishable instances, such as those created from distinct
77 constructor invocations (See JLS {@jls value-objects-15.9.4}), may be no
78 longer distinguishable via {@code ==}.
79 <li>The use of value objects for synchronization or with {@linkplain
80 java.lang.ref.Reference object references} results in {@link IdentityException}.
81 </ul>
82
83 </body>
84 </html>
|