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