Boolean (Java SE 17 & JDK 17) (original) (raw)
All Implemented Interfaces:
[Serializable](../io/Serializable.html "interface in java.io"), [Comparable](Comparable.html "interface in java.lang")<[Boolean](Boolean.html "class in java.lang")>, [Constable](constant/Constable.html "interface in java.lang.constant")
The Boolean class wraps a value of the primitive typeboolean in an object. An object of typeBoolean contains a single field whose type isboolean.
In addition, this class provides many methods for converting a boolean to a String and aString to a boolean, as well as other constants and methods useful when dealing with aboolean.
This is a value-based class; programmers should treat instances that areequal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.
Since:
1.0
See Also:
Field Summary
Fields
The Boolean object corresponding to the primitive value false.
The Boolean object corresponding to the primitive value true.
The Class object representing the primitive type boolean.
Constructor Summary
Constructors[Boolean](#%3Cinit%3E%28boolean%29)(boolean value)
Deprecated, for removal: This API element is subject to removal in a future version.
Deprecated, for removal: This API element is subject to removal in a future version.
Method Summary
boolean
Returns the value of this Boolean object as a boolean primitive.static int[compare](#compare%28boolean,boolean%29)(boolean x, boolean y)
Compares two boolean values.int
Compares this Boolean instance with another.
Returns an Optional containing the nominal descriptor for this instance.boolean
Returns true if and only if the argument is notnull and is a Boolean object that represents the same boolean value as this object.static boolean
Returns true if and only if the system property named by the argument exists and is equal to, ignoring case, the string "true".int[hashCode](#hashCode%28%29)()
Returns a hash code for this Boolean object.static int[hashCode](#hashCode%28boolean%29)(boolean value)
Returns a hash code for a boolean value; compatible withBoolean.hashCode().static boolean[logicalAnd](#logicalAnd%28boolean,boolean%29)(boolean a, boolean b)
Returns the result of applying the logical AND operator to the specified boolean operands.static boolean[logicalOr](#logicalOr%28boolean,boolean%29)(boolean a, boolean b)
Returns the result of applying the logical OR operator to the specified boolean operands.static boolean[logicalXor](#logicalXor%28boolean,boolean%29)(boolean a, boolean b)
Returns the result of applying the logical XOR operator to the specified boolean operands.static boolean
Parses the string argument as a boolean.[toString](#toString%28%29)()
Returns a String object representing this Boolean's value.[toString](#toString%28boolean%29)(boolean b)
Returns a String object representing the specified boolean.[valueOf](#valueOf%28boolean%29)(boolean b)
Returns a Boolean instance representing the specifiedboolean value.
Returns a Boolean with a value represented by the specified string.
Field Details
TRUE
public static final Boolean TRUE
TheBooleanobject corresponding to the primitive valuetrue.FALSE
public static final Boolean FALSE
TheBooleanobject corresponding to the primitive valuefalse.TYPE
The Class object representing the primitive type boolean.
Since:
1.1Constructor Details
Boolean
Deprecated, for removal: This API element is subject to removal in a future version.
Allocates aBooleanobject representing thevalueargument.
Parameters:
value- the value of theBoolean.Boolean
Deprecated, for removal: This API element is subject to removal in a future version.
Allocates aBooleanobject representing the valuetrueif the string argument is notnulland is equal, ignoring case, to the string"true". Otherwise, allocates aBooleanobject representing the valuefalse.
Parameters:
s- the string to be converted to aBoolean.Method Details
parseBoolean
public static boolean parseBoolean(String s)
Parses the string argument as a boolean. Thebooleanreturned represents the valuetrueif the string argument is notnulland is equal, ignoring case, to the string"true". Otherwise, a false value is returned, including for a null argument.
Example:Boolean.parseBoolean("True")returnstrue.
Example:Boolean.parseBoolean("yes")returnsfalse.
Parameters:
s- theStringcontaining the boolean representation to be parsed
Returns:
the boolean represented by the string argument
Since:
1.5booleanValue
public boolean booleanValue()
Returns the value of thisBooleanobject as a boolean primitive.
Returns:
the primitivebooleanvalue of this object.valueOf
public static Boolean valueOf(boolean b)
Returns aBooleaninstance representing the specifiedbooleanvalue. If the specifiedbooleanvalue istrue, this method returnsBoolean.TRUE; if it isfalse, this method returnsBoolean.FALSE. If a newBooleaninstance is not required, this method should generally be used in preference to the constructorBoolean(boolean), as this method is likely to yield significantly better space and time performance.
Parameters:
b- a boolean value.
Returns:
aBooleaninstance representingb.
Since:
1.4valueOf
Returns a
Booleanwith a value represented by the specified string. TheBooleanreturned represents a true value if the string argument is notnulland is equal, ignoring case, to the string"true". Otherwise, a false value is returned, including for a null argument.
Parameters:
s- a string.
Returns:
theBooleanvalue represented by the string.toString
public static String toString(boolean b)
Returns aStringobject representing the specified boolean. If the specified boolean istrue, then the string"true"will be returned, otherwise the string"false"will be returned.
Parameters:
b- the boolean to be converted
Returns:
the string representation of the specifiedboolean
Since:
1.4toString
Returns a
Stringobject representing this Boolean's value. If this object represents the valuetrue, a string equal to"true"is returned. Otherwise, a string equal to"false"is returned.
Overrides:
[toString](Object.html#toString%28%29)in class[Object](Object.html "class in java.lang")
Returns:
a string representation of this object.hashCode
public int hashCode()
Returns a hash code for thisBooleanobject.
Overrides:
[hashCode](Object.html#hashCode%28%29)in class[Object](Object.html "class in java.lang")
Returns:
the integer1231if this object representstrue; returns the integer1237if this object representsfalse.
See Also:
* Object.equals(java.lang.Object)
* System.identityHashCode(java.lang.Object)hashCode
public static int hashCode(boolean value)
Returns a hash code for abooleanvalue; compatible withBoolean.hashCode().
Parameters:
value- the value to hash
Returns:
a hash code value for abooleanvalue.
Since:
1.8equals
public boolean equals(Object obj)
Returnstrueif and only if the argument is notnulland is aBooleanobject that represents the samebooleanvalue as this object.
Overrides:
[equals](Object.html#equals%28java.lang.Object%29)in class[Object](Object.html "class in java.lang")
Parameters:
obj- the object to compare with.
Returns:
trueif the Boolean objects represent the same value;falseotherwise.
See Also:
* Object.hashCode()
* HashMapgetBoolean
public static boolean getBoolean(String name)
Returnstrueif and only if the system property named by the argument exists and is equal to, ignoring case, the string"true". A system property is accessible throughgetProperty, a method defined by theSystemclass.
If there is no property with the specified name, or if the specified name is empty or null, thenfalseis returned.
Parameters:
name- the system property name.
Returns:
thebooleanvalue of the system property.
Throws:
[SecurityException](SecurityException.html "class in java.lang")- for the same reasons asSystem.getProperty
See Also:
* System.getProperty(java.lang.String)
* System.getProperty(java.lang.String, java.lang.String)compareTo
public int compareTo(Boolean b)
Compares thisBooleaninstance with another.
Specified by:
[compareTo](Comparable.html#compareTo%28T%29)in interface[Comparable](Comparable.html "interface in java.lang")<[Boolean](Boolean.html "class in java.lang")>
Parameters:
b- theBooleaninstance to be compared
Returns:
zero if this object represents the same boolean value as the argument; a positive value if this object represents true and the argument represents false; and a negative value if this object represents false and the argument represents true
Throws:
[NullPointerException](NullPointerException.html "class in java.lang")- if the argument isnull
Since:
1.5
See Also:
* Comparablecompare
public static int compare(boolean x, boolean y)
Compares twobooleanvalues. The value returned is identical to what would be returned by:
Boolean.valueOf(x).compareTo(Boolean.valueOf(y))Parameters:
x- the firstbooleanto compare
y- the secondbooleanto compare
Returns:
the value0ifx == y; a value less than0if!x && y; and a value greater than0ifx && !y
Since:
1.7logicalAnd
public static boolean logicalAnd(boolean a, boolean b)
Returns the result of applying the logical AND operator to the specifiedbooleanoperands.
Parameters:
a- the first operand
b- the second operand
Returns:
the logical AND ofaandb
Since:
1.8
See Also:
* BinaryOperatorlogicalOr
public static boolean logicalOr(boolean a, boolean b)
Returns the result of applying the logical OR operator to the specifiedbooleanoperands.
Parameters:
a- the first operand
b- the second operand
Returns:
the logical OR ofaandb
Since:
1.8
See Also:
* BinaryOperatorlogicalXor
public static boolean logicalXor(boolean a, boolean b)
Returns the result of applying the logical XOR operator to the specifiedbooleanoperands.
Parameters:
a- the first operand
b- the second operand
Returns:
the logical XOR ofaandb
Since:
1.8
See Also:
* BinaryOperatordescribeConstable
Returns an Optional containing the nominal descriptor for this instance.
Specified by:
[describeConstable](constant/Constable.html#describeConstable%28%29)in interface[Constable](constant/Constable.html "interface in java.lang.constant")
Returns:
an Optional describing the Boolean instance
Since:
15