In java programming we use primitive type call Boolean. It can only return two possible value that is true and false. In programming some time it’s important to create an output that can use to control other operations, Boolean is help us on it.
Now let’s write a program
Now let’s write a program
public class useBooleans {
public static void main(String args[])
{
boolean a;
a=true;//set a as true
System.out.println("a is "+a);
a=false;//set a as false
System.out.println("a is "+a);
}
}
Let’s see the output of this programpublic static void main(String args[])
{
boolean a;
a=true;//set a as true
System.out.println("a is "+a);
a=false;//set a as false
System.out.println("a is "+a);
}
}
a is true
a is false
As we can see that when we set the value of variable a as ‘true’ it returns a as ‘true’. Same happen for ‘false’. Now if we are use this value in programming with conditional operators like ‘if-else’ or ‘while’ then it makes proper use of Boolean Variables. Let’s write a programa is false
public class useBooleans {
public static void main(String args[])
{
boolean a;
a=true;//set a as true
if(a)
{
System.out.println("This is executed when a "+a);
}
a=false;//set a as false
if(a)
{
System.out.println("This is executed when a "+a);
}
}
}
Let’s see the output of this programpublic static void main(String args[])
{
boolean a;
a=true;//set a as true
if(a)
{
System.out.println("This is executed when a "+a);
}
a=false;//set a as false
if(a)
{
System.out.println("This is executed when a "+a);
}
}
}
This is executed when a true
In above program we can see that ‘if’ is executed when variable a is true. In programming relational operators return Boolean variable. For example we can write a program
public class useBooleans {
public static void main(String args[])
{
int a=5,b=9;
System.out.println("a>b is "+(a>b));
}
}
Let’s see the output of above programpublic static void main(String args[])
{
int a=5,b=9;
System.out.println("a>b is "+(a>b));
}
}
a>b is false
in above program we can see that (a>b) is printed as false.
