Tuesday, 18 November 2014

Use Boolean in java programming

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
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 program
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 program
 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 program
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 program
a>b is false
in above program we can see that (a>b) is printed as false.

7 comments:

  1. I really liked your blog article.Really thank you! Really Cool.Web Development course in Kolhapur

    ReplyDelete
  2. Thanks for the post. It was worth reading.
    also, check Java Training in Pune

    ReplyDelete