Source Home >> Java Source 1.6.0 >> java.lang.Boolean V 0.09
  • 001/*
  • 002 * @(#)Boolean.java 1.53 05/11/17
  • 003 *
  • 004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 006 */
  • 007
  • 008package java.lang;
  • 009
  • 010/**
  • 011 * The Boolean class wraps a value of the primitive type
  • 012 * <code>boolean</code> in an object. An object of type
  • 013 * <code>Boolean</code> contains a single field whose type is
  • 014 * <code>boolean</code>.
  • 015 * <p>
  • 016 * In addition, this class provides many methods for
  • 017 * converting a <code>boolean</code> to a <code>String</code> and a
  • 018 * <code>String</code> to a <code>boolean</code>, as well as other
  • 019 * constants and methods useful when dealing with a
  • 020 * <code>boolean</code>.
  • 021 *
  • 022 * @author Arthur van Hoff
  • 023 * @version 1.53, 11/17/05
  • 024 * @since JDK1.0
  • 025 */
  • 026public final class Boolean implements java.io.Serializable,
  • 027 Comparable<Boolean>
  • 028{
  • 029 /**
  • 030 * The <code>Boolean</code> object corresponding to the primitive
  • 031 * value <code>true</code>.
  • 032 */
  • 033 public static final Boolean TRUE = new Boolean(true);
  • 034
  • 035 /**
  • 036 * The <code>Boolean</code> object corresponding to the primitive
  • 037 * value <code>false</code>.
  • 038 */
  • 039 public static final Boolean FALSE = new Boolean(false);
  • 040
  • 041 /**
  • 042 * The Class object representing the primitive type boolean.
  • 043 *
  • 044 * @since JDK1.1
  • 045 */
  • 046 public static final Class<Boolean> TYPE = Class.getPrimitiveClass("boolean");
  • 047
  • 048 /**
  • 049 * The value of the Boolean.
  • 050 *
  • 051 * @serial
  • 052 */
  • 053 private final boolean value;
  • 054
  • 055 /** use serialVersionUID from JDK 1.0.2 for interoperability */
  • 056 private static final long serialVersionUID = -3665804199014368530L;
  • 057
  • 058 /**
  • 059 * Allocates a <code>Boolean</code> object representing the
  • 060 * <code>value</code> argument.
  • 061 *
  • 062 * <p><b>Note: It is rarely appropriate to use this constructor.
  • 063 * Unless a <i>new</i> instance is required, the static factory
  • 064 * {@link #valueOf(boolean)} is generally a better choice. It is
  • 065 * likely to yield significantly better space and time performance.</b>
  • 066 *
  • 067 * @param value the value of the <code>Boolean</code>.
  • 068 */
  • 069 public Boolean(boolean value) {
  • 070 this.value = value;
  • 071 }
  • 072
  • 073 /**
  • 074 * Allocates a <code>Boolean</code> object representing the value
  • 075 * <code>true</code> if the string argument is not <code>null</code>
  • 076 * and is equal, ignoring case, to the string {@code "true"}.
  • 077 * Otherwise, allocate a <code>Boolean</code> object representing the
  • 078 * value <code>false</code>. Examples:<p>
  • 079 * {@code new Boolean("True")} produces a <tt>Boolean</tt> object
  • 080 * that represents <tt>true</tt>.<br>
  • 081 * {@code new Boolean("yes")} produces a <tt>Boolean</tt> object
  • 082 * that represents <tt>false</tt>.
  • 083 *
  • 084 * @param s the string to be converted to a <code>Boolean</code>.
  • 085 */
  • 086 public Boolean(String s) {
  • 087 this(toBoolean(s));
  • 088 }
  • 089
  • 090 /**
  • 091 * Parses the string argument as a boolean. The <code>boolean</code>
  • 092 * returned represents the value <code>true</code> if the string argument
  • 093 * is not <code>null</code> and is equal, ignoring case, to the string
  • 094 * {@code "true"}. <p>
  • 095 * Example: {@code Boolean.parseBoolean("True")} returns <tt>true</tt>.<br>
  • 096 * Example: {@code Boolean.parseBoolean("yes")} returns <tt>false</tt>.
  • 097 *
  • 098 * @param s the <code>String</code> containing the boolean
  • 099 * representation to be parsed
  • 100 * @return the boolean represented by the string argument
  • 101 * @since 1.5
  • 102 */
  • 103 public static boolean parseBoolean(String s) {
  • 104 return toBoolean(s);
  • 105 }
  • 106
  • 107 /**
  • 108 * Returns the value of this <tt>Boolean</tt> object as a boolean
  • 109 * primitive.
  • 110 *
  • 111 * @return the primitive <code>boolean</code> value of this object.
  • 112 */
  • 113 public boolean booleanValue() {
  • 114 return value;
  • 115 }
  • 116
  • 117 /**
  • 118 * Returns a <tt>Boolean</tt> instance representing the specified
  • 119 * <tt>boolean</tt> value. If the specified <tt>boolean</tt> value
  • 120 * is <tt>true</tt>, this method returns <tt>Boolean.TRUE</tt>;
  • 121 * if it is <tt>false</tt>, this method returns <tt>Boolean.FALSE</tt>.
  • 122 * If a new <tt>Boolean</tt> instance is not required, this method
  • 123 * should generally be used in preference to the constructor
  • 124 * {@link #Boolean(boolean)}, as this method is likely to yield
  • 125 * significantly better space and time performance.
  • 126 *
  • 127 * @param b a boolean value.
  • 128 * @return a <tt>Boolean</tt> instance representing <tt>b</tt>.
  • 129 * @since 1.4
  • 130 */
  • 131 public static Boolean valueOf(boolean b) {
  • 132 return (b ? TRUE : FALSE);
  • 133 }
  • 134
  • 135 /**
  • 136 * Returns a <code>Boolean</code> with a value represented by the
  • 137 * specified string. The <code>Boolean</code> returned represents a
  • 138 * true value if the string argument is not <code>null</code>
  • 139 * and is equal, ignoring case, to the string {@code "true"}.
  • 140 *
  • 141 * @param s a string.
  • 142 * @return the <code>Boolean</code> value represented by the string.
  • 143 */
  • 144 public static Boolean valueOf(String s) {
  • 145 return toBoolean(s) ? TRUE : FALSE;
  • 146 }
  • 147
  • 148 /**
  • 149 * Returns a <tt>String</tt> object representing the specified
  • 150 * boolean. If the specified boolean is <code>true</code>, then
  • 151 * the string {@code "true"} will be returned, otherwise the
  • 152 * string {@code "false"} will be returned.
  • 153 *
  • 154 * @param b the boolean to be converted
  • 155 * @return the string representation of the specified <code>boolean</code>
  • 156 * @since 1.4
  • 157 */
  • 158 public static String toString(boolean b) {
  • 159 return b ? "true" : "false";
  • 160 }
  • 161
  • 162 /**
  • 163 * Returns a <tt>String</tt> object representing this Boolean's
  • 164 * value. If this object represents the value <code>true</code>,
  • 165 * a string equal to {@code "true"} is returned. Otherwise, a
  • 166 * string equal to {@code "false"} is returned.
  • 167 *
  • 168 * @return a string representation of this object.
  • 169 */
  • 170 public String toString() {
  • 171 return value ? "true" : "false";
  • 172 }
  • 173
  • 174 /**
  • 175 * Returns a hash code for this <tt>Boolean</tt> object.
  • 176 *
  • 177 * @return the integer <tt>1231</tt> if this object represents
  • 178 * <tt>true</tt>; returns the integer <tt>1237</tt> if this
  • 179 * object represents <tt>false</tt>.
  • 180 */
  • 181 public int hashCode() {
  • 182 return value ? 1231 : 1237;
  • 183 }
  • 184
  • 185 /**
  • 186 * Returns <code>true</code> if and only if the argument is not
  • 187 * <code>null</code> and is a <code>Boolean</code> object that
  • 188 * represents the same <code>boolean</code> value as this object.
  • 189 *
  • 190 * @param obj the object to compare with.
  • 191 * @return <code>true</code> if the Boolean objects represent the
  • 192 * same value; <code>false</code> otherwise.
  • 193 */
  • 194 public boolean equals(Object obj) {
  • 195 if (obj instanceof Boolean) {
  • 196 return value == ((Boolean)obj).booleanValue();
  • 197 }
  • 198 return false;
  • 199 }
  • 200
  • 201 /**
  • 202 * Returns <code>true</code> if and only if the system property
  • 203 * named by the argument exists and is equal to the string
  • 204 * {@code "true"}. (Beginning with version 1.0.2 of the
  • 205 * Java<small><sup>TM</sup></small> platform, the test of
  • 206 * this string is case insensitive.) A system property is accessible
  • 207 * through <code>getProperty</code>, a method defined by the
  • 208 * <code>System</code> class.
  • 209 * <p>
  • 210 * If there is no property with the specified name, or if the specified
  • 211 * name is empty or null, then <code>false</code> is returned.
  • 212 *
  • 213 * @param name the system property name.
  • 214 * @return the <code>boolean</code> value of the system property.
  • 215 * @see java.lang.System#getProperty(java.lang.String)
  • 216 * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  • 217 */
  • 218 public static boolean getBoolean(String name) {
  • 219 boolean result = false;
  • 220 try {
  • 221 result = toBoolean(System.getProperty(name));
  • 222 } catch (IllegalArgumentException e) {
  • 223 } catch (NullPointerException e) {
  • 224 }
  • 225 return result;
  • 226 }
  • 227
  • 228 /**
  • 229 * Compares this <tt>Boolean</tt> instance with another.
  • 230 *
  • 231 * @param b the <tt>Boolean</tt> instance to be compared
  • 232 * @return zero if this object represents the same boolean value as the
  • 233 * argument; a positive value if this object represents true
  • 234 * and the argument represents false; and a negative value if
  • 235 * this object represents false and the argument represents true
  • 236 * @throws NullPointerException if the argument is <tt>null</tt>
  • 237 * @see Comparable
  • 238 * @since 1.5
  • 239 */
  • 240 public int compareTo(Boolean b) {
  • 241 return (b.value == value ? 0 : (value ? 1 : -1));
  • 242 }
  • 243
  • 244 private static boolean toBoolean(String name) {
  • 245 return ((name != null) && name.equalsIgnoreCase("true"));
  • 246 }
  • 247}

文件:Boolean.java
包名:java.lang
类名:Boolean
继承:
接口:[java.io.Serializable][Comparable]