Path: blob/master/src/java.base/share/classes/java/lang/Boolean.java
41152 views
/*1* Copyright (c) 1994, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;2627import jdk.internal.vm.annotation.IntrinsicCandidate;2829import java.lang.constant.Constable;30import java.lang.constant.ConstantDesc;31import java.lang.constant.ConstantDescs;32import java.lang.constant.DynamicConstantDesc;33import java.util.Optional;3435import static java.lang.constant.ConstantDescs.BSM_GET_STATIC_FINAL;36import static java.lang.constant.ConstantDescs.CD_Boolean;3738/**39* The Boolean class wraps a value of the primitive type40* {@code boolean} in an object. An object of type41* {@code Boolean} contains a single field whose type is42* {@code boolean}.43*44* <p>In addition, this class provides many methods for45* converting a {@code boolean} to a {@code String} and a46* {@code String} to a {@code boolean}, as well as other47* constants and methods useful when dealing with a48* {@code boolean}.49*50* <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>51* class; programmers should treat instances that are52* {@linkplain #equals(Object) equal} as interchangeable and should not53* use instances for synchronization, or unpredictable behavior may54* occur. For example, in a future release, synchronization may fail.55*56* @author Arthur van Hoff57* @since 1.058*/59@jdk.internal.ValueBased60public final class Boolean implements java.io.Serializable,61Comparable<Boolean>, Constable62{63/**64* The {@code Boolean} object corresponding to the primitive65* value {@code true}.66*/67public static final Boolean TRUE = new Boolean(true);6869/**70* The {@code Boolean} object corresponding to the primitive71* value {@code false}.72*/73public static final Boolean FALSE = new Boolean(false);7475/**76* The Class object representing the primitive type boolean.77*78* @since 1.179*/80@SuppressWarnings("unchecked")81public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean");8283/**84* The value of the Boolean.85*86* @serial87*/88private final boolean value;8990/** use serialVersionUID from JDK 1.0.2 for interoperability */91@java.io.Serial92private static final long serialVersionUID = -3665804199014368530L;9394/**95* Allocates a {@code Boolean} object representing the96* {@code value} argument.97*98* @param value the value of the {@code Boolean}.99*100* @deprecated101* It is rarely appropriate to use this constructor. The static factory102* {@link #valueOf(boolean)} is generally a better choice, as it is103* likely to yield significantly better space and time performance.104* Also consider using the final fields {@link #TRUE} and {@link #FALSE}105* if possible.106*/107@Deprecated(since="9", forRemoval = true)108public Boolean(boolean value) {109this.value = value;110}111112/**113* Allocates a {@code Boolean} object representing the value114* {@code true} if the string argument is not {@code null}115* and is equal, ignoring case, to the string {@code "true"}.116* Otherwise, allocates a {@code Boolean} object representing the117* value {@code false}.118*119* @param s the string to be converted to a {@code Boolean}.120*121* @deprecated122* It is rarely appropriate to use this constructor.123* Use {@link #parseBoolean(String)} to convert a string to a124* {@code boolean} primitive, or use {@link #valueOf(String)}125* to convert a string to a {@code Boolean} object.126*/127@Deprecated(since="9", forRemoval = true)128public Boolean(String s) {129this(parseBoolean(s));130}131132/**133* Parses the string argument as a boolean. The {@code boolean}134* returned represents the value {@code true} if the string argument135* is not {@code null} and is equal, ignoring case, to the string136* {@code "true"}.137* Otherwise, a false value is returned, including for a null138* argument.<p>139* Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>140* Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.141*142* @param s the {@code String} containing the boolean143* representation to be parsed144* @return the boolean represented by the string argument145* @since 1.5146*/147public static boolean parseBoolean(String s) {148return "true".equalsIgnoreCase(s);149}150151/**152* Returns the value of this {@code Boolean} object as a boolean153* primitive.154*155* @return the primitive {@code boolean} value of this object.156*/157@IntrinsicCandidate158public boolean booleanValue() {159return value;160}161162/**163* Returns a {@code Boolean} instance representing the specified164* {@code boolean} value. If the specified {@code boolean} value165* 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 method168* should generally be used in preference to the constructor169* {@link #Boolean(boolean)}, as this method is likely to yield170* 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.4175*/176@IntrinsicCandidate177public static Boolean valueOf(boolean b) {178return (b ? TRUE : FALSE);179}180181/**182* Returns a {@code Boolean} with a value represented by the183* specified string. The {@code Boolean} returned represents a184* 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 null187* argument.188*189* @param s a string.190* @return the {@code Boolean} value represented by the string.191*/192public static Boolean valueOf(String s) {193return parseBoolean(s) ? TRUE : FALSE;194}195196/**197* Returns a {@code String} object representing the specified198* boolean. If the specified boolean is {@code true}, then199* the string {@code "true"} will be returned, otherwise the200* string {@code "false"} will be returned.201*202* @param b the boolean to be converted203* @return the string representation of the specified {@code boolean}204* @since 1.4205*/206public static String toString(boolean b) {207return b ? "true" : "false";208}209210/**211* Returns a {@code String} object representing this Boolean's212* value. If this object represents the value {@code true},213* a string equal to {@code "true"} is returned. Otherwise, a214* string equal to {@code "false"} is returned.215*216* @return a string representation of this object.217*/218public String toString() {219return value ? "true" : "false";220}221222/**223* Returns a hash code for this {@code Boolean} object.224*225* @return the integer {@code 1231} if this object represents226* {@code true}; returns the integer {@code 1237} if this227* object represents {@code false}.228*/229@Override230public int hashCode() {231return Boolean.hashCode(value);232}233234/**235* Returns a hash code for a {@code boolean} value; compatible with236* {@code Boolean.hashCode()}.237*238* @param value the value to hash239* @return a hash code value for a {@code boolean} value.240* @since 1.8241*/242public static int hashCode(boolean value) {243return value ? 1231 : 1237;244}245246/**247* Returns {@code true} if and only if the argument is not248* {@code null} and is a {@code Boolean} object that249* represents the same {@code boolean} value as this object.250*251* @param obj the object to compare with.252* @return {@code true} if the Boolean objects represent the253* same value; {@code false} otherwise.254*/255public boolean equals(Object obj) {256if (obj instanceof Boolean) {257return value == ((Boolean)obj).booleanValue();258}259return false;260}261262/**263* Returns {@code true} if and only if the system property named264* by the argument exists and is equal to, ignoring case, the265* string {@code "true"}.266* A system property is accessible through {@code getProperty}, a267* method defined by the {@code System} class. <p> If there is no268* property with the specified name, or if the specified name is269* empty or null, then {@code false} is returned.270*271* @param name the system property name.272* @return the {@code boolean} value of the system property.273* @throws SecurityException for the same reasons as274* {@link System#getProperty(String) System.getProperty}275* @see java.lang.System#getProperty(java.lang.String)276* @see java.lang.System#getProperty(java.lang.String, java.lang.String)277*/278public static boolean getBoolean(String name) {279boolean result = false;280try {281result = parseBoolean(System.getProperty(name));282} catch (IllegalArgumentException | NullPointerException e) {283}284return result;285}286287/**288* Compares this {@code Boolean} instance with another.289*290* @param b the {@code Boolean} instance to be compared291* @return zero if this object represents the same boolean value as the292* argument; a positive value if this object represents true293* and the argument represents false; and a negative value if294* this object represents false and the argument represents true295* @throws NullPointerException if the argument is {@code null}296* @see Comparable297* @since 1.5298*/299public int compareTo(Boolean b) {300return compare(this.value, b.value);301}302303/**304* Compares two {@code boolean} values.305* The value returned is identical to what would be returned by:306* <pre>307* Boolean.valueOf(x).compareTo(Boolean.valueOf(y))308* </pre>309*310* @param x the first {@code boolean} to compare311* @param y the second {@code boolean} to compare312* @return the value {@code 0} if {@code x == y};313* a value less than {@code 0} if {@code !x && y}; and314* a value greater than {@code 0} if {@code x && !y}315* @since 1.7316*/317public static int compare(boolean x, boolean y) {318return (x == y) ? 0 : (x ? 1 : -1);319}320321/**322* Returns the result of applying the logical AND operator to the323* specified {@code boolean} operands.324*325* @param a the first operand326* @param b the second operand327* @return the logical AND of {@code a} and {@code b}328* @see java.util.function.BinaryOperator329* @since 1.8330*/331public static boolean logicalAnd(boolean a, boolean b) {332return a && b;333}334335/**336* Returns the result of applying the logical OR operator to the337* specified {@code boolean} operands.338*339* @param a the first operand340* @param b the second operand341* @return the logical OR of {@code a} and {@code b}342* @see java.util.function.BinaryOperator343* @since 1.8344*/345public static boolean logicalOr(boolean a, boolean b) {346return a || b;347}348349/**350* Returns the result of applying the logical XOR operator to the351* specified {@code boolean} operands.352*353* @param a the first operand354* @param b the second operand355* @return the logical XOR of {@code a} and {@code b}356* @see java.util.function.BinaryOperator357* @since 1.8358*/359public static boolean logicalXor(boolean a, boolean b) {360return a ^ b;361}362363/**364* Returns an {@link Optional} containing the nominal descriptor for this365* instance.366*367* @return an {@link Optional} describing the {@linkplain Boolean} instance368* @since 15369*/370@Override371public Optional<DynamicConstantDesc<Boolean>> describeConstable() {372return Optional.of(value ? ConstantDescs.TRUE : ConstantDescs.FALSE);373}374}375376377