< prev index next >

src/java.base/share/classes/java/lang/Boolean.java

Print this page

  1 /*
  2  * Copyright (c) 1994, 2025, 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.ConstantDescs;
 32 import java.lang.constant.DynamicConstantDesc;
 33 import java.util.Optional;
 34 
 35 /**
 36  * The {@code Boolean} class is the {@linkplain
 37  * java.lang##wrapperClass wrapper class} for values of the primitive
 38  * type {@code boolean}. An object of type {@code Boolean} contains a
 39  * single field whose type is {@code boolean}.
 40  *
 41  * <p>In addition, this class provides many methods for
 42  * converting a {@code boolean} to a {@code String} and a
 43  * {@code String} to a {@code boolean}, as well as other
 44  * constants and methods useful when dealing with a
 45  * {@code boolean}.
 46  *
 47  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 48  * class; programmers should treat instances that are
 49  * {@linkplain #equals(Object) equal} as interchangeable and should not
 50  * use instances for synchronization, or unpredictable behavior may
 51  * occur. For example, in a future release, synchronization may fail.








 52  *
 53  * @author  Arthur van Hoff
 54  * @since   1.0
 55  */

 56 @jdk.internal.ValueBased
 57 public final class Boolean implements java.io.Serializable,
 58                                       Comparable<Boolean>, Constable
 59 {
 60     /**
 61      * The {@code Boolean} object corresponding to the primitive
 62      * value {@code true}.
 63      */
 64     public static final Boolean TRUE = new Boolean(true);
 65 
 66     /**
 67      * The {@code Boolean} object corresponding to the primitive
 68      * value {@code false}.
 69      */
 70     public static final Boolean FALSE = new Boolean(false);
 71 
 72     /**
 73      * The Class object representing the primitive type boolean.
 74      *
 75      * @since   1.1
 76      */
 77     public static final Class<Boolean> TYPE = Class.getPrimitiveClass("boolean");
 78 

 84     private final boolean value;
 85 
 86     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 87     @java.io.Serial
 88     private static final long serialVersionUID = -3665804199014368530L;
 89 
 90     /**
 91      * Allocates a {@code Boolean} object representing the
 92      * {@code value} argument.
 93      *
 94      * @param   value   the value of the {@code Boolean}.
 95      *
 96      * @deprecated
 97      * It is rarely appropriate to use this constructor. The static factory
 98      * {@link #valueOf(boolean)} is generally a better choice, as it is
 99      * likely to yield significantly better space and time performance.
100      * Also consider using the final fields {@link #TRUE} and {@link #FALSE}
101      * if possible.
102      */
103     @Deprecated(since="9")

104     public Boolean(boolean value) {
105         this.value = value;
106     }
107 
108     /**
109      * Allocates a {@code Boolean} object representing the value
110      * {@code true} if the string argument is not {@code null}
111      * and is equal, ignoring case, to the string {@code "true"}.
112      * Otherwise, allocates a {@code Boolean} object representing the
113      * value {@code false}.
114      *
115      * @param   s   the string to be converted to a {@code Boolean}.
116      *
117      * @deprecated
118      * It is rarely appropriate to use this constructor.
119      * Use {@link #parseBoolean(String)} to convert a string to a
120      * {@code boolean} primitive, or use {@link #valueOf(String)}
121      * to convert a string to a {@code Boolean} object.
122      */
123     @Deprecated(since="9")

140      * @return     the boolean represented by the string argument
141      * @since 1.5
142      */
143     public static boolean parseBoolean(String s) {
144         return "true".equalsIgnoreCase(s);
145     }
146 
147     /**
148      * Returns the value of this {@code Boolean} object as a boolean
149      * primitive.
150      *
151      * @return  the primitive {@code boolean} value of this object.
152      */
153     @IntrinsicCandidate
154     public boolean booleanValue() {
155         return value;
156     }
157 
158     /**
159      * Returns a {@code Boolean} instance representing the specified
160      * {@code boolean} value.  If the specified {@code boolean} value
161      * is {@code true}, this method returns {@code Boolean.TRUE};
162      * if it is {@code false}, this method returns {@code Boolean.FALSE}.
163      * If a new {@code Boolean} instance is not required, this method
164      * should generally be used in preference to the constructor
165      * {@link #Boolean(boolean)}, as this method is likely to yield
166      * significantly better space and time performance.













167      *
168      * @param  b a boolean value.
169      * @return a {@code Boolean} instance representing {@code b}.
170      * @since  1.4
171      */
172     @IntrinsicCandidate
173     public static Boolean valueOf(boolean b) {
174         return (b ? TRUE : FALSE);
175     }
176 
177     /**
178      * Returns a {@code Boolean} with a value represented by the
179      * specified string.  The {@code Boolean} returned represents a
180      * true value if the string argument is not {@code null}
181      * and is equal, ignoring case, to the string {@code "true"}.
182      * Otherwise, a false value is returned, including for a null
183      * argument.
184      *
185      * @param   s   a string.
186      * @return  the {@code Boolean} value represented by the string.

  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.MigratedValueClass
 66 @jdk.internal.ValueBased
 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 

 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")

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.
< prev index next >