1 /*
2 * Copyright (c) 1994, 2026, 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.value.Deserializer;
29 import jdk.internal.vm.annotation.IntrinsicCandidate;
30
31 import java.lang.constant.Constable;
32 import java.lang.constant.ConstantDescs;
33 import java.lang.constant.DynamicConstantDesc;
34 import java.util.Optional;
35
36 /**
37 * The {@code Boolean} class is the {@linkplain
38 * java.lang##wrapperClass wrapper class} for values of the primitive
39 * type {@code boolean}. An object of type {@code Boolean} contains a
40 * single field whose type is {@code boolean}.
41 *
42 * <p>In addition, this class provides many methods for
43 * converting a {@code boolean} to a {@code String} and a
44 * {@code String} to a {@code boolean}, as well as other
45 * constants and methods useful when dealing with a
46 * {@code boolean}.
47 *
48 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
49 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
50 * as interchangeable and should not use instances for synchronization, mutexes, or
51 * with {@linkplain java.lang.ref.Reference object references}.
52 *
53 * <div class="preview-block">
54 * <div class="preview-comment">
55 * When preview features are enabled, {@code Boolean} is a {@linkplain Class#isValue value class}.
56 * Use of value class instances for synchronization, mutexes, or with
57 * {@linkplain java.lang.ref.Reference object references} result in
58 * {@link IdentityException}.
59 * </div>
60 * </div>
61 *
62 * @author Arthur van Hoff
63 * @since 1.0
64 */
65 @jdk.internal.ValueBased
66 public final /*value*/ class Boolean
67 implements java.io.Serializable, Comparable<Boolean>, Constable
68 {
69 /**
70 * The {@code Boolean} object corresponding to the primitive
71 * value {@code true}.
72 */
73 public static final Boolean TRUE = new Boolean(true);
74
75 /**
76 * The {@code Boolean} object corresponding to the primitive
77 * value {@code false}.
78 */
79 public static final Boolean FALSE = new Boolean(false);
80
81 /**
82 * The Class object representing the primitive type boolean.
83 *
84 * @since 1.1
85 */
86 public static final Class<Boolean> TYPE = Class.getPrimitiveClass("boolean");
87
88 /**
89 * The value of the Boolean.
90 *
91 * @serial
92 */
93 private final boolean value;
94
95 /** use serialVersionUID from JDK 1.0.2 for interoperability */
96 @java.io.Serial
97 private static final long serialVersionUID = -3665804199014368530L;
98
99 /**
100 * Allocates a {@code Boolean} object representing the
101 * {@code value} argument.
102 *
103 * @param value the value of the {@code Boolean}.
104 *
105 * @deprecated
106 * It is rarely appropriate to use this constructor. The static factory
107 * {@link #valueOf(boolean)} is generally a better choice, as it is
108 * likely to yield significantly better space and time performance.
109 * Also consider using the final fields {@link #TRUE} and {@link #FALSE}
110 * if possible.
111 */
112 @Deprecated(since="9")
113 @Deserializer("value")
114 public Boolean(boolean value) {
115 this.value = value;
116 }
117
118 /**
119 * Allocates a {@code Boolean} object representing the value
120 * {@code true} if the string argument is not {@code null}
121 * and is equal, ignoring case, to the string {@code "true"}.
122 * Otherwise, allocates a {@code Boolean} object representing the
123 * value {@code false}.
124 *
125 * @param s the string to be converted to a {@code Boolean}.
126 *
127 * @deprecated
128 * It is rarely appropriate to use this constructor.
129 * Use {@link #parseBoolean(String)} to convert a string to a
130 * {@code boolean} primitive, or use {@link #valueOf(String)}
131 * to convert a string to a {@code Boolean} object.
132 */
133 @Deprecated(since="9")
134 public Boolean(String s) {
135 this(parseBoolean(s));
136 }
137
138 /**
139 * Parses the string argument as a boolean. The {@code boolean}
140 * returned represents the value {@code true} if the string argument
141 * is not {@code null} and is equal, ignoring case, to the string
142 * {@code "true"}.
143 * Otherwise, a false value is returned, including for a null
144 * argument.<p>
145 * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
146 * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
147 *
148 * @param s the {@code String} containing the boolean
149 * representation to be parsed
150 * @return the boolean represented by the string argument
151 * @since 1.5
152 */
153 public static boolean parseBoolean(String s) {
154 return "true".equalsIgnoreCase(s);
155 }
156
157 /**
158 * Returns the value of this {@code Boolean} object as a boolean
159 * primitive.
160 *
161 * @return the primitive {@code boolean} value of this object.
162 */
163 @IntrinsicCandidate
164 public boolean booleanValue() {
165 return value;
166 }
167
168 /**
169 * Returns a {@code Boolean} instance representing the specified
170 * {@code boolean} value.
171 * <div class="preview-block">
172 * <div class="preview-comment">
173 * <p>
174 * - When preview features are NOT enabled, {@code Boolean} is an identity class.
175 * If the specified {@code boolean} value is {@code true},
176 * this method returns {@code Boolean.TRUE}; if it is
177 * {@code false}, this method returns {@code Boolean.FALSE}.
178 * If a new {@code Boolean} instance is not required, this
179 * method should generally be used in preference to the
180 * constructor {@link #Boolean(boolean)}, as this method is
181 * likely to yield significantly better space and time
182 * performance.
183 * </p>
184 * <p>
185 * - When preview features are enabled, {@code Boolean} is a {@linkplain Class#isValue value class}.
186 * The {@code valueOf} behavior is the same as invoking the constructor.
187 * </p>
188 * </div>
189 * </div>
190 *
191 * @param b a boolean value.
192 * @return a {@code Boolean} instance representing {@code b}.
193 * @since 1.4
194 */
195 @IntrinsicCandidate
196 public static Boolean valueOf(boolean b) {
197 return (b ? TRUE : FALSE);
198 }
199
200 /**
201 * Returns a {@code Boolean} with a value represented by the
202 * specified string. The {@code Boolean} returned represents a
203 * true value if the string argument is not {@code null}
204 * and is equal, ignoring case, to the string {@code "true"}.
205 * Otherwise, a false value is returned, including for a null
206 * argument.
207 *
208 * @param s a string.
209 * @return the {@code Boolean} value represented by the string.
210 */
211 public static Boolean valueOf(String s) {
212 return parseBoolean(s) ? TRUE : FALSE;
213 }
214
215 /**
216 * Returns a {@code String} object representing the specified
217 * boolean. If the specified boolean is {@code true}, then
218 * the string {@code "true"} will be returned, otherwise the
219 * string {@code "false"} will be returned.
220 *
221 * @param b the boolean to be converted
222 * @return the string representation of the specified {@code boolean}
223 * @since 1.4
224 */
225 public static String toString(boolean b) {
226 return String.valueOf(b);
227 }
228
229 /**
230 * Returns a {@code String} object representing this Boolean's
231 * value. If this object represents the value {@code true},
232 * a string equal to {@code "true"} is returned. Otherwise, a
233 * string equal to {@code "false"} is returned.
234 *
235 * @return a string representation of this object.
236 */
237 @Override
238 public String toString() {
239 return String.valueOf(value);
240 }
241
242 /**
243 * Returns a hash code for this {@code Boolean} object.
244 *
245 * @return the integer {@code 1231} if this object represents
246 * {@code true}; returns the integer {@code 1237} if this
247 * object represents {@code false}.
248 */
249 @Override
250 public int hashCode() {
251 return Boolean.hashCode(value);
252 }
253
254 /**
255 * Returns a hash code for a {@code boolean} value; compatible with
256 * {@code Boolean.hashCode()}.
257 *
258 * @param value the value to hash
259 * @return a hash code value for a {@code boolean} value.
260 * @since 1.8
261 */
262 public static int hashCode(boolean value) {
263 return value ? 1231 : 1237;
264 }
265
266 /**
267 * Returns {@code true} if and only if the argument is not
268 * {@code null} and is a {@code Boolean} object that
269 * represents the same {@code boolean} value as this object.
270 *
271 * @param obj the object to compare with.
272 * @return {@code true} if the Boolean objects represent the
273 * same value; {@code false} otherwise.
274 */
275 public boolean equals(Object obj) {
276 if (obj instanceof Boolean b) {
277 return value == b.booleanValue();
278 }
279 return false;
280 }
281
282 /**
283 * Returns {@code true} if and only if the system property named
284 * by the argument exists and is equal to, ignoring case, the
285 * string {@code "true"}.
286 * A system property is accessible through {@code getProperty}, a
287 * method defined by the {@code System} class. <p> If there is no
288 * property with the specified name, or if the specified name is
289 * empty or null, then {@code false} is returned.
290 *
291 * @param name the system property name.
292 * @return the {@code boolean} value of the system property.
293 * @see java.lang.System#getProperty(java.lang.String)
294 * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
295 */
296 public static boolean getBoolean(String name) {
297 return name != null && !name.isEmpty() && parseBoolean(System.getProperty(name));
298 }
299
300 /**
301 * Compares this {@code Boolean} instance with another.
302 *
303 * @param b the {@code Boolean} instance to be compared
304 * @return zero if this object represents the same boolean value as the
305 * argument; a positive value if this object represents true
306 * and the argument represents false; and a negative value if
307 * this object represents false and the argument represents true
308 * @throws NullPointerException if the argument is {@code null}
309 * @see Comparable
310 * @since 1.5
311 */
312 public int compareTo(Boolean b) {
313 return compare(this.value, b.value);
314 }
315
316 /**
317 * Compares two {@code boolean} values.
318 * The value returned is identical to what would be returned by:
319 * <pre>
320 * Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
321 * </pre>
322 *
323 * @param x the first {@code boolean} to compare
324 * @param y the second {@code boolean} to compare
325 * @return the value {@code 0} if {@code x == y};
326 * a value less than {@code 0} if {@code !x && y}; and
327 * a value greater than {@code 0} if {@code x && !y}
328 * @since 1.7
329 */
330 public static int compare(boolean x, boolean y) {
331 return (x == y) ? 0 : (x ? 1 : -1);
332 }
333
334 /**
335 * Returns the result of applying the logical AND operator to the
336 * specified {@code boolean} operands.
337 *
338 * @param a the first operand
339 * @param b the second operand
340 * @return the logical AND of {@code a} and {@code b}
341 * @see java.util.function.BinaryOperator
342 * @since 1.8
343 */
344 public static boolean logicalAnd(boolean a, boolean b) {
345 return a && b;
346 }
347
348 /**
349 * Returns the result of applying the logical OR operator to the
350 * specified {@code boolean} operands.
351 *
352 * @param a the first operand
353 * @param b the second operand
354 * @return the logical OR of {@code a} and {@code b}
355 * @see java.util.function.BinaryOperator
356 * @since 1.8
357 */
358 public static boolean logicalOr(boolean a, boolean b) {
359 return a || b;
360 }
361
362 /**
363 * Returns the result of applying the logical XOR operator to the
364 * specified {@code boolean} operands.
365 *
366 * @param a the first operand
367 * @param b the second operand
368 * @return the logical XOR of {@code a} and {@code b}
369 * @see java.util.function.BinaryOperator
370 * @since 1.8
371 */
372 public static boolean logicalXor(boolean a, boolean b) {
373 return a ^ b;
374 }
375
376 /**
377 * Returns an {@link Optional} containing the nominal descriptor for this
378 * instance.
379 *
380 * @return an {@link Optional} describing the {@linkplain Boolean} instance
381 * @since 15
382 */
383 @Override
384 public Optional<DynamicConstantDesc<Boolean>> describeConstable() {
385 return Optional.of(value ? ConstantDescs.TRUE : ConstantDescs.FALSE);
386 }
387 }