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