Java.lang.Boolean Class in Java (original) (raw)

Last Updated : 13 Oct, 2022

Java provides a wrapper class Boolean in java.lang package. The 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 useful methods like to convert a boolean to a String and a String to a boolean, while dealing with a boolean variable.

Creating a Boolean object

Boolean class provides two constructors for creating Boolean object.

Boolean b = new Boolean(boolean value);

Boolean b = new Boolean(String s);

Fields:

Methods:

Syntax : public static boolean parseBoolean(String s) Parameters : s - the String containing the boolean representation to be parsed Returns : the boolean represented by the string argument

Java

public class Test

{

`` public static void main(String[] args)

`` {

`` boolean b1 = Boolean.parseBoolean("True");

`` boolean b2 = Boolean.parseBoolean("TruE");

`` boolean b3 = Boolean.parseBoolean("False");

`` boolean b4 = Boolean.parseBoolean("FALSE");

`` boolean b5 = Boolean.parseBoolean("GeeksForGeeks");

`` System.out.println(b1);

`` System.out.println(b2);

`` System.out.println(b3);

`` System.out.println(b4);

`` System.out.println(b5);

`` }

}

Output:

true true false false false

Syntax : public boolean booleanValue() Parameters : NA Returns : the primitive boolean value of this object.

Java

public class Test

{

`` public static void main(String[] args)

`` {

`` Boolean b1 = new Boolean("True");

`` Boolean b2 = new Boolean("False");

`` Boolean b3 = new Boolean("GeeksForGeeks");

`` boolean b4 = b1.booleanValue();

`` boolean b5 = b2.booleanValue();

`` boolean b6 = b3.booleanValue();

`` System.out.println(b4);

`` System.out.println(b5);

`` System.out.println(b6);

`` }

}

Output:

true false false

Syntax : public static boolean valueOf(boolean b) Parameters : b - a boolean value. Returns : a Boolean object representing b.

Java

public class Test

{

`` public static void main(String[] args)

`` {

`` boolean b1 = true ;

`` boolean b2 = false ;

`` Boolean b3 = Boolean.valueOf(b1);

`` Boolean b4 = Boolean.valueOf(b2);

`` System.out.println(b3);

`` System.out.println(b4);

`` }

}

Output:

true false

Syntax : public static boolean valueOf(String s) Parameters : s - a string Returns : a Boolean value represented by the string

Java

public class Test

{

`` public static void main(String[] args)

`` {

`` Boolean b1 = Boolean.valueOf(" true ");

`` Boolean b2 = Boolean.valueOf("TRue");

`` Boolean b3 = Boolean.valueOf("False");

`` Boolean b4 = Boolean.valueOf("GeeksForGeeks");

`` Boolean b5 = Boolean.valueOf( null );

`` System.out.println(b1);

`` System.out.println(b2);

`` System.out.println(b3);

`` System.out.println(b4);

`` System.out.println(b5);

`` }

}

Output:

true true false false false

Syntax : public static String toString(boolean b) Parameters : b - the boolean to be converted Returns : the string representation of the specified boolean

Java

public class Test

{

`` public static void main(String[] args)

`` {

`` boolean b1 = true ;

`` boolean b2 = false ;

`` String str1 = Boolean.toString(b1);

`` String str2 = Boolean.toString(b2);

`` System.out.println(str1);

`` System.out.println(str2);

`` }

}

Output:

true false

Syntax : public String toString() Parameters : NA Returns : a string representation of this object Overrides : toString in class Object

Java

public class Test

{

`` public static void main(String[] args)

`` {

`` Boolean b1 = new Boolean("True");

`` Boolean b2 = new Boolean("False");

`` Boolean b3 = new Boolean("GeeksForGeeks");

`` Boolean b4 = new Boolean( null );

`` String str1 = b1.toString();

`` String str2 = b2.toString();

`` String str3 = b3.toString();

`` String str4 = b4.toString();

`` System.out.println(str1);

`` System.out.println(str2);

`` System.out.println(str3);

`` System.out.println(str4);

`` }

}

Output:

true false false false

Syntax : public int hashCode() Parameters : NA Returns : the integer 1231 if this object represents true; returns the integer 1237 if this object represents false Overrides : hashCode in class Object

Java

public class Test

{

`` public static void main(String[] args)

`` {

`` Boolean b1 = new Boolean("True");

`` Boolean b2 = new Boolean("False");

`` Boolean b3 = new Boolean("TRue");

`` Boolean b4 = new Boolean( null );

`` System.out.println(b1.hashCode());

`` System.out.println(b2.hashCode());

`` System.out.println(b3.hashCode());

`` System.out.println(b4.hashCode());

`` }

}

Output:

1231 1237 1231 1237

Syntax : public boolean equals(Object obj) Parameters : obj - the object to compare with. Returns : true if the Boolean objects represent the same value; false otherwise Overrides : equals in class Object

Java

public class Test

{

`` public static void main(String[] args)

`` {

`` Boolean b1 = new Boolean("True");

`` Boolean b2 = new Boolean("False");

`` Boolean b3 = new Boolean("TrUe");

`` Boolean b4 = new Boolean( null );

`` System.out.println(b1.equals(b2));

`` System.out.println(b2.equals(b4));

`` System.out.println(b1.equals(b3));

`` System.out.println(b1.equals(b4));

`` }

}

Output:

false true true false

Syntax : public int compareTo(Boolean b) Parameters : b - the Boolean instance 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; a negative value if this object represents false and the argument represents true. Throws : NullPointerException - if the argument is null

Java

public class Test

{

`` public static void main(String[] args)

`` {

`` Boolean b1 = new Boolean("True");

`` Boolean b2 = new Boolean("False");

`` Boolean b3 = new Boolean("TRue");

`` Boolean b4 = new Boolean( null );

`` System.out.println(b1.compareTo(b2));

`` System.out.println(b1.compareTo(b3));

`` System.out.println(b2.compareTo(b1));

`` System.out.println(b1.compareTo(b4));

`` System.out.println(b2.compareTo(b4));

`` }

}

Output:

1 0 -1 1 0

Syntax : public static int compare(boolean x, boolean y) Parameters : x - the first boolean to compare y - the second boolean to compare Returns : zero if x is same boolean value as y; a positive value x is true and y is false; a negative value if x is false and y is true; Throws : NullPointerException - if the argument is null

Java

public class Test

{

`` public static void main(String[] args)

`` {

`` boolean b1 = true ;

`` boolean b2 = false ;

`` boolean b3 = true ;

`` boolean b4 = false ;

`` System.out.println(Boolean.compare(b1, b2));

`` System.out.println(Boolean.compare(b1, b3));

`` System.out.println(Boolean.compare(b2, b1));

`` System.out.println(Boolean.compare(b2, b4));

`` }

}

Output:

1 0 -1 0