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