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