java.lang.Boolean Class

The Boolean class wraps the value of primitive data type boolean into Boolean object. An object of type Boolean contains a single field whose type is boolean. In other words the wrapper classes create objects for primitive data types. The wrapper classes are Boolean, Byte, Character, Short, Integer, Float, Long and Double.

This class has following constructors.

Boolean Class Constructors

Boolean(boolean value)

Allocates a Boolean object representing the value argument.

Boolean(String s)

Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true".

java.lang.Boolean Class Example
/* Boolean Class Example */
/* Save with file name BooleanExample.java */

public class BooleanExample
{
	public static void main(String args[])
	{
		//BOOLEAN DECLARATION
		Boolean b;
		//MEMORY ALLOCATION FOR BOOLEAN
		b = new Boolean(true);
		//BOOLEAN CLASS OUTPUT
		System.out.println();
		System.out.println("Boolean Class Example");
		System.out.println("=====================");
		System.out.println();
		System.out.println("Value is : "+ b.booleanValue());
	}
}
                        
java.lang.Boolean Class Example 2
/* Save with file name BooleanExample2.java */

public class BooleanExample2
{
	public static void main(String args[])
	{
		//BOOLEAN DECLARATION
		Boolean b, b2;
		//MEMORY ALLOCATION FOR BOOLEAN
		b = new Boolean(true);
		b2 = new Boolean("false");
		//BOOLEAN CLASS OUTPUT
		System.out.println();
		System.out.println("Boolean Class Example");
		System.out.println("=====================");
		System.out.println();
		System.out.println("b Value is : "+ b.booleanValue());
		System.out.println("b2 Value is : "+ b2.booleanValue());
		System.out.println();
		System.out.println("b compareTo b2 : "+ b.compareTo(b2));
		System.out.println("b equals b2 : "+ b.equals(b2));
	}
}
                        

In the following example you can learn how to use static methods of Boolean Class. If you use static methods you need not to create the class instance. You can call static methods using the class name as reference.

java.lang.Boolean Class Example 3
/* Save with file name BooleanExample3.java */

public class BooleanExample3
{
	public static void main(String args[])
	{
		//BOOLEAN CLASS STATIC METHODS USAGE
		System.out.println("Boolean Class Example");
		System.out.println("=====================");
		System.out.println();
		//RETURN Boolean OBJECT WITH THE SPECIFIED PRIMITIVE boolean DATA TYPE
		System.out.println("valueOf : "+ Boolean.valueOf(false));
		//RETURN Boolean OBJECT WITH THE SPECIFIED
		//PRIMITIVE boolean DATA TYPE AS String
		System.out.println("valueOf : "+ Boolean.valueOf("true"));
		//RETURN PRIMITIVE boolean DATA TYPE WITH THE SPECIFIED
		//PRIMITIVE boolean DATA TYPE AS String
		System.out.println("getBoolean : "+ Boolean.getBoolean("true"));
	}
}