Java Boolean With Examples

In programming, we often require a data type that can have only two possible values (e.g Yes/No, True/False) and is bound to have any one of them for sure. This requirement is served by data type boolean.

As discussed previously, boolean is a primitive data type that can store either true or false. Its size is 1-bit. The default value of boolean is false.   

Example:

boolean myBool = true;     //Declaration of variable myBool that has value true
boolean myBool1;         //Default declaration, so myBool1 has default value i.e false

Boolean expression

Any java expression that yields a boolean value is called a Boolean expression. The boolean expression has its utility in Java control statements comprising conditions and comparisons, where we need to take a decision on the basis of the output that Boolean expression gives.

Example:

The following are Boolean expression have either true or false as output post-evaluation.

Expression Description
9>13   9 is not greater than 13. Hence, is false
3<2^3 3 is less than 2^3 i.e 8. Hence, is true   
7==7 Notice the difference, we are using == instead of = to check if it is left operand is equal to the right operand. The reason will be revealed in the Java Operators lecture.

Let us see the boolean expression in a java program.

public class MyBoolean
{
	public static void main(String args[])
	{
		int x=9;
		int y=12;

		System.out.println("x = "+x);
		System.out.println("y = "+y);

		System.out.println("Is x > y? Answer:"+(x>y)); //Here the x>y is a Boolean expression
		System.out.println("Is x = y? Answer:"+(x==y)); //Here the x==y is a Boolean expression
		System.out.println("Is x < y? Answer:"+(x<y)); //Here the x<y is a Boolean expression
	}
}

Output:

x = 9
y = 12
Is x > y? Answer:false
Is x = y? Answer:false
Is x < y? Answer:true

Boolean Class

Just like other data types, we have a class Boolean, which is an alternative to the primitive data type boolean. This class provides a method to convert boolean to String and vice versa. In our previous lectures also, we could see so many methods that yield boolean output. Similar to other primitive data types, we can see the implicit conversion of the primitive data type to its wrapper class and vice versa to be applicable in boolean as well. There are two ways to create a Boolean object.

Constructor :

There are two different versions of constructors available for object creation.

Example 1:

Boolean myBooleanObject = new Boolean(Boolean value);

This is rarely used unless a new instance is required. There is a method valueOf() which we will discuss in the methods section. It a better option in terms of performance.

Example 2:

Boolean myBooleanObject = new Boolean(String inputString);

The Boolean object is allocated a value on the basis of inputString. If inputString is not null, myBooleanObject holds true, else false.

Autoboxing :

Boolean myBool = false;
Boolean myBool2 = true;

Boolean Methods

Boolean provides a wide range of methods used very frequently. Let us have a look at a few of them:

Java.lang.Boolean.parseBoolean :

Name: parseBoolean(String inputString)
Return: boolean
Purpose: parses inputString as boolean. The result is true only if inputString is found to be true ignoring case-sensitivity

Example:

public class MyBoolean
{
	public static void main(String args[])
	{
		System.out.println(Boolean.parseBoolean("TRUE"));
		System.out.println(Boolean.parseBoolean("true"));
		System.out.println(Boolean.parseBoolean("Hello"));
	}
}

Output:

true
true
false

Java.lang.Boolean.toString() :

Name: toString()
Return: String
Purpose: It returns a String object representing the calling Boolean object’s value.

Example:

public class MyBoolean
{
	public static void main(String args[])
	{
		Boolean myBoolVar = true;
		System.out.println(myBoolVar.toString());
	}
}

Output:

true

Java.lang.Boolean.toString(Boolean inputBoolean) :

Name: toString(Boolean inputBoolean)
Return: String
Purpose: It returns a String object representing the calling Boolean object’s value.

Example:

public class MyBoolean
{
	public static void main(String args[])
	{
		System.out.println(Boolean.toString(true));
		System.out.println(Boolean.toString(8>5));
		System.out.println(Boolean.toString(3==6));    
	}
}

Output:

true
true
false

Java.lang.Boolean.valueOf(boolean inputBoolean) :

Name: valueOf(boolean inputBoolean)
Return: Boolean
Purpose: Returns a Boolean object representing the inputBoolean value. It is an alternative to the constructor as stated earlier.

Example:

public class MyBoolean
{
	public static void main(String args[])
	{
		System.out.println(Boolean.valueOf(true));
		System.out.println(Boolean.valueOf(false));
	}
}

Output:

true
false

Java.lang.Boolean.valueOf(String  inputString) :

Name: valueOf(String  inputString)
Return: Boolean
Purpose: It returns a Boolean object representing the inputString.

Example:

public class MyBoolean
{
	public static void main(String args[])
	{
		System.out.println(Boolean.valueOf("xyz"));
		System.out.println(Boolean.valueOf("true"));
		System.out.println(Boolean.valueOf("True"));
	}
}

Output:

false
true
true

Java.lang.Boolean.compare(boolean firstInput, boolean secondInput) :

Name: compare(boolean firstInput, boolean secondInput)
Return: int
Purpose: It compares the input Boolean values

Example:

public class MyBoolean
{
	public static void main(String args[])
	{
		System.out.println(Boolean.compare(true,true));
		System.out.println(Boolean.compare(true,false));
	}
}

Output:

0
1

Java.lang.Boolean.compareTo(Boolean boolInput) :

Name: compareTo(Boolean boolInput)
Return: int
Purpose: It compares the input Boolean values with calling Boolean object

Example:

public class MyBoolean
{
	public static void main(String args[])
	{
		Boolean val = true;
		System.out.println(val.compareTo(true));
		System.out.println(val.compareTo(false));
	}
}

Output:

0
1

This was an overview of Boolean Class. In the next lecture, we will discuss Java Math.