Google+

43. 'boolean' data type






'boolean' variable can only hold one of two possible values, true or false.



Program demonstrating the boolean type:

class BoolTest1
{
   public static void main(String args[])
   {
      boolean b=false;
      System.out.println(" b is " +b);

      b=true;
      System.out.println(" b is " +b);

    }
 }

Output of this program:

b is false
b is true



Program demonstrating that a boolean value can control the if statement
class BoolTest2
{
   public static void main(String args[])
   {
      boolean b=true;

      if(b)
      System.out.println("This is executed");

      b=false;

      if(b)
      System.out.println("This is not executed");

    }
 }
      
Output of this program:

This is executed



Program demonstrating that the outcome of a relational operator is a boolean value

class BoolTest3
{
   public static void main(String args[])
   {

      System.out.println("10 > 9 is "+(10>9));

   }
 }

Output of this program:

10 > 9 is true






Please comment below to feedback or ask questions.

The next post will explain how to use the data types correctly by assigning the proper values to the variables of any type.




8 comments:

Tanu said...

Hi Arun,

at each of ur blog pages, i am doing some hands on myself to learn the basic well.
so on this page i thgt i will explore the for loop and i wrote a simple program, intend to write odd numbers between 1 to 30
but it gives complition error .. can you help?
public class PracticeIf {

//this i am writing to do hands on with If statement
public static void main(String[] args)
{
//i will write odd numbers between 1 to 30
int i = 1 , j = 2, k ;
System.out.println(" first odd number is " + i);
i++;
for (i = 2;i < 30;i++)
{
k = i % j;

if (k==0)
{
System.out.println("next odd number is" + i);
}
}
}
}

Arun Motoori said...

@Tanu - I didn't get any compiler error from your code. I have modified your code to print the odd numbers. Please find the updated code below -

public class PracticeIf {

public static void main(String[] args) {

int i = 1 , j = 2, k ;
System.out.println("first odd number is " + i);
i++;
for (i = 2;i < 30;i++){
k = i % j;

if (k!=0) {
System.out.println("next odd number is" + i);
}
}
}
}

Tanu said...

Thanks Arun.. i saw u used not equal which is correct
and the compilation error was of a semicolon missing
thanks

Unknown said...

hi arun am confused, for the below program!Please help me!
Program demonstrating that the outcome of a relational operator is a boolean value
class BoolTest3
{
public static void main(String args[])
{
System.out.println("10 > 9 is "+(10>9));
}
}
Output of this program:
10 > 9 is true

How the Output takes the value as true?

Arun Motoori said...

@Tijoy - The following statements return either true or false

10 > 9 -> returns true
10 < 9 -> returns false

10 == 10 -> returns true
10 != 10 -> returns false

You can store the values into a boolean type variable and print

boolean b = 10 > 9; //assigns true to variable b
System.out.println(b); //prints true

Or else you can directly print the result using

System.out.println(10>9); //prints true


Unknown said...

Thanks Arun! Understood the logic! :)

Unknown said...

Hi .. Arun,
I am not getting the flow of execution of second program.Can you brief more on this?

Unknown said...

Hi Arun,

Did not understand the flow of second program, also tried executing it but got error response as "could not find or load main class BoolTes2"

class BoolTest2
{
public static void main(String args[])
{
boolean b=true;

if(b)
System.out.println("This is executed");

b=false;

if(b)
System.out.println("This is not executed");

}
}