1 /*
2 * Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
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.vm.annotation.IntrinsicCandidate;
29
30 import java.lang.constant.Constable;
31 import java.lang.constant.ConstantDesc;
32 import java.lang.constant.ConstantDescs;
33 import java.lang.constant.DynamicConstantDesc;
34 import java.util.Optional;
35
36 import static java.lang.constant.ConstantDescs.BSM_GET_STATIC_FINAL;
37 import static java.lang.constant.ConstantDescs.CD_Boolean;
38
39 /**
40 * The Boolean class wraps a value of the primitive type
41 * {@code boolean} in an object. An object of type
42 * {@code Boolean} contains a single field whose type is
43 * {@code boolean}.
44 *
45 * <p>In addition, this class provides many methods for
46 * converting a {@code boolean} to a {@code String} and a
47 * {@code String} to a {@code boolean}, as well as other
48 * constants and methods useful when dealing with a
49 * {@code boolean}.
50 *
51 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
52 * class; programmers should treat instances that are
53 * {@linkplain #equals(Object) equal} as interchangeable and should not
54 * use instances for synchronization, or unpredictable behavior may
55 * occur. For example, in a future release, synchronization may fail.
56 *
57 * @author Arthur van Hoff
58 * @since 1.0
59 */
60 @jdk.internal.ValueBased
61 public final class Boolean implements java.io.Serializable,
62 Comparable<Boolean>, Constable
63 {
64 /**
65 * The {@code Boolean} object corresponding to the primitive
66 * value {@code true}.
67 */
68 public static final Boolean TRUE = new Boolean(true);
69
70 /**
71 * The {@code Boolean} object corresponding to the primitive
72 * value {@code false}.
73 */
74 public static final Boolean FALSE = new Boolean(false);
75
76 /**
77 * The Class object representing the primitive type boolean.
78 *
79 * @since 1.1
80 */
81 @SuppressWarnings("unchecked")
82 public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean");
83
84 /**
85 * The value of the Boolean.
86 *
87 * @serial
88 */
89 private final boolean value;
90
91 /** use serialVersionUID from JDK 1.0.2 for interoperability */
92 @java.io.Serial
93 private static final long serialVersionUID = -3665804199014368530L;
94
|
1 /*
2 * Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
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.vm.annotation.IntrinsicCandidate;
29
30 import java.lang.constant.Constable;
31 import java.lang.constant.ConstantDesc;
32 import java.lang.constant.ConstantDescs;
33 import java.lang.constant.DynamicConstantDesc;
34 import java.util.Optional;
35
36 import static java.lang.constant.ConstantDescs.BSM_GET_STATIC_FINAL;
37 import static java.lang.constant.ConstantDescs.CD_Boolean;
38
39 import jdk.internal.misc.CDS;
40
41 /**
42 * The Boolean class wraps a value of the primitive type
43 * {@code boolean} in an object. An object of type
44 * {@code Boolean} contains a single field whose type is
45 * {@code boolean}.
46 *
47 * <p>In addition, this class provides many methods for
48 * converting a {@code boolean} to a {@code String} and a
49 * {@code String} to a {@code boolean}, as well as other
50 * constants and methods useful when dealing with a
51 * {@code boolean}.
52 *
53 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
54 * class; programmers should treat instances that are
55 * {@linkplain #equals(Object) equal} as interchangeable and should not
56 * use instances for synchronization, or unpredictable behavior may
57 * occur. For example, in a future release, synchronization may fail.
58 *
59 * @author Arthur van Hoff
60 * @since 1.0
61 */
62 @jdk.internal.ValueBased
63 public final class Boolean implements java.io.Serializable,
64 Comparable<Boolean>, Constable
65 {
66 // CDS support.
67 private static Boolean[] archivedCache;
68 static {
69 CDS.initializeFromArchive(Boolean.class);
70 }
71
72 /**
73 * The {@code Boolean} object corresponding to the primitive
74 * value {@code true}.
75 */
76 public static final Boolean TRUE = (archivedCache != null) ? archivedCache[0] : new Boolean(true);
77
78 /**
79 * The {@code Boolean} object corresponding to the primitive
80 * value {@code false}.
81 */
82 public static final Boolean FALSE = (archivedCache != null) ? archivedCache[1] : new Boolean(false);
83
84 static {
85 if (archivedCache == null) {
86 archivedCache = new Boolean[2];
87 archivedCache[0] = TRUE;
88 archivedCache[1] = FALSE;
89 }
90 }
91
92 /**
93 * The Class object representing the primitive type boolean.
94 *
95 * @since 1.1
96 */
97 @SuppressWarnings("unchecked")
98 public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean");
99
100 /**
101 * The value of the Boolean.
102 *
103 * @serial
104 */
105 private final boolean value;
106
107 /** use serialVersionUID from JDK 1.0.2 for interoperability */
108 @java.io.Serial
109 private static final long serialVersionUID = -3665804199014368530L;
110
|