Boolean
class wraps a value of the primitive type
boolean
in an object. An object of type
Boolean
contains a single field whose type is
boolean
.
In addition, this class provides many methods for converting a
boolean
to a
String
and a
String
to a
boolean
, as well as other constants and methods useful when dealing with a
boolean
.
Boolean Constructors :
Boolean Methods :
Boolean
class BooleanTest
{
public static void main(String arg[])
{
Boolean a = new Boolean(true);
Boolean b = new Boolean(false);
Boolean c = new Boolean(false);
System.out.println("Boolean value of a : " + a.booleanValue());
System.out.println("Comparison of b and c using compare : " + Boolean.compare(b, c));
System.out.println("CompareTo of a and c : " + a.compareTo(c)); // LINE A
System.out.println("Equals of b and c : " + b.equals(c));
System.setProperty("Isboolean", "true"); // LINE B
System.out.println("Get Boolean from Isboolean : " + Boolean.getBoolean("Isboolean"));
System.out.println("Hash code for Boolean : " + a.hashCode()); // LINE C
System.out.println("ParseBoolean converts String toboolean : " + Boolean.parseBoolean("true"));
System.out.println("Boolean is converted into String : " + b.toString());
System.out.println("Boolean is converted into String : " + Boolean.toString(a));
System.out.println("Converts Boolean into Boolean : " + Boolean.valueOf(a));
System.out.println("Converts String into Boolean : " + Boolean.valueOf("true"));
}
}
OUTPUTBoolean value of a : true
Compare of b and c : 0
CompareTo of a and c : 1
Equals of b and c : true
Get Boolean from property : true
Hash code for Boolean : 1231
ParseBoolean converts String : true
Boolean is converted into String : false
Boolean is converted into String : true
Converts Boolean into Boolean : true
Converts String into Boolean : true
DESCRIPTIONIn this program, all the Boolean
methods used to obtain Boolean
value.
THINGS TO TRY
- At
LINE A
, interchange the variables a, c
in compareTo
method and see the output difference.
- At
LINE B
, in place of "true"
put "false"
and see the output difference.
- At
LINE C
, replace a
by b
and see the hash code difference.