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