< prev index next >

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

Print this page

  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 {@code Boolean} class is the {@linkplain
 41  * java.lang##wrapperClass wrapper class} for values of the primitive
 42  * type {@code boolean}. An object of type {@code Boolean} contains a
 43  * single field whose type is {@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

157     @IntrinsicCandidate
158     public boolean booleanValue() {
159         return value;
160     }
161 
162     /**
163      * Returns a {@code Boolean} instance representing the specified
164      * {@code boolean} value.  If the specified {@code boolean} value
165      * is {@code true}, this method returns {@code Boolean.TRUE};
166      * if it is {@code false}, this method returns {@code Boolean.FALSE}.
167      * If a new {@code Boolean} instance is not required, this method
168      * should generally be used in preference to the constructor
169      * {@link #Boolean(boolean)}, as this method is likely to yield
170      * significantly better space and time performance.
171      *
172      * @param  b a boolean value.
173      * @return a {@code Boolean} instance representing {@code b}.
174      * @since  1.4
175      */
176     @IntrinsicCandidate

177     public static Boolean valueOf(boolean b) {
178         return (b ? TRUE : FALSE);



179     }
180 
181     /**
182      * Returns a {@code Boolean} with a value represented by the
183      * specified string.  The {@code Boolean} returned represents a
184      * true value if the string argument is not {@code null}
185      * and is equal, ignoring case, to the string {@code "true"}.
186      * Otherwise, a false value is returned, including for a null
187      * argument.
188      *
189      * @param   s   a string.
190      * @return  the {@code Boolean} value represented by the string.
191      */
192     public static Boolean valueOf(String s) {
193         return parseBoolean(s) ? TRUE : FALSE;
194     }
195 
196     /**
197      * Returns a {@code String} object representing the specified
198      * boolean.  If the specified boolean is {@code true}, then

  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.misc.PreviewFeatures;
 29 import jdk.internal.value.DeserializeConstructor;
 30 import jdk.internal.vm.annotation.IntrinsicCandidate;
 31 
 32 import java.lang.constant.Constable;

 33 import java.lang.constant.ConstantDescs;
 34 import java.lang.constant.DynamicConstantDesc;
 35 import java.util.Optional;
 36 



 37 /**
 38  * The {@code Boolean} class is the {@linkplain
 39  * java.lang##wrapperClass wrapper class} for values of the primitive
 40  * type {@code boolean}. An object of type {@code Boolean} contains a
 41  * single field whose type is {@code boolean}.
 42  *
 43  * <p>In addition, this class provides many methods for
 44  * converting a {@code boolean} to a {@code String} and a
 45  * {@code String} to a {@code boolean}, as well as other
 46  * constants and methods useful when dealing with a
 47  * {@code boolean}.
 48  *
 49  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 50  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
 51  * as interchangeable and should not use instances for synchronization, mutexes, or
 52  * with {@linkplain java.lang.ref.Reference object references}.
 53  *
 54  * <div class="preview-block">
 55  *      <div class="preview-comment">
 56  *          When preview features are enabled, {@code Boolean} is a {@linkplain Class#isValue value class}.
 57  *          Use of value class instances for synchronization, mutexes, or with
 58  *          {@linkplain java.lang.ref.Reference object references} result in
 59  *          {@link IdentityException}.
 60  *      </div>
 61  * </div>
 62  *
 63  * @author  Arthur van Hoff
 64  * @since   1.0
 65  */
 66 @jdk.internal.MigratedValueClass
 67 @jdk.internal.ValueBased
 68 public final class Boolean implements java.io.Serializable,
 69                                       Comparable<Boolean>, Constable
 70 {
 71     /**
 72      * The {@code Boolean} object corresponding to the primitive
 73      * value {@code true}.
 74      */
 75     public static final Boolean TRUE = new Boolean(true);
 76 
 77     /**
 78      * The {@code Boolean} object corresponding to the primitive
 79      * value {@code false}.
 80      */
 81     public static final Boolean FALSE = new Boolean(false);
 82 
 83     /**
 84      * The Class object representing the primitive type boolean.
 85      *
 86      * @since   1.1

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.  If the specified {@code boolean} value
172      * is {@code true}, this method returns {@code Boolean.TRUE};
173      * if it is {@code false}, this method returns {@code Boolean.FALSE}.
174      * If a new {@code Boolean} instance is not required, this method
175      * should generally be used in preference to the constructor
176      * {@link #Boolean(boolean)}, as this method is likely to yield
177      * significantly better space and time performance.
178      *
179      * @param  b a boolean value.
180      * @return a {@code Boolean} instance representing {@code b}.
181      * @since  1.4
182      */
183     @IntrinsicCandidate
184     @DeserializeConstructor
185     public static Boolean valueOf(boolean b) {
186         if (!PreviewFeatures.isEnabled()) {
187             return (b ? TRUE : FALSE);
188         }
189         return new Boolean(b);
190     }
191 
192     /**
193      * Returns a {@code Boolean} with a value represented by the
194      * specified string.  The {@code Boolean} returned represents a
195      * true value if the string argument is not {@code null}
196      * and is equal, ignoring case, to the string {@code "true"}.
197      * Otherwise, a false value is returned, including for a null
198      * argument.
199      *
200      * @param   s   a string.
201      * @return  the {@code Boolean} value represented by the string.
202      */
203     public static Boolean valueOf(String s) {
204         return parseBoolean(s) ? TRUE : FALSE;
205     }
206 
207     /**
208      * Returns a {@code String} object representing the specified
209      * boolean.  If the specified boolean is {@code true}, then
< prev index next >