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.
boolean myBool = true; //Declaration of variable myBool that has value true
boolean myBool1; //Default declaration, so myBool1 has default value i.e false
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.
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
}
}
x = 9
y = 12
Is x > y? Answer:false
Is x = y? Answer:false
Is x < y? Answer:true
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.
There are two different versions of constructors available for object creation.
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.
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.
Boolean myBool = false;
Boolean myBool2 = true;
Boolean provides a wide range of methods used very frequently. Let us have a look at a few of them:
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"));
}
}
true
true
false
public class MyBoolean
{
public static void main(String args[])
{
Boolean myBoolVar = true;
System.out.println(myBoolVar.toString());
}
}
true
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));
}
}
true
true
false
public class MyBoolean
{
public static void main(String args[])
{
System.out.println(Boolean.valueOf(true));
System.out.println(Boolean.valueOf(false));
}
}
true
false
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"));
}
}
false
true
true
public class MyBoolean
{
public static void main(String args[])
{
System.out.println(Boolean.compare(true,true));
System.out.println(Boolean.compare(true,false));
}
}
0
1
public class MyBoolean
{
public static void main(String args[])
{
Boolean val = true;
System.out.println(val.compareTo(true));
System.out.println(val.compareTo(false));
}
}
0
1
This was an overview of Boolean Class. In the next lecture, we will discuss Java Math.