• Register
140 points
4

In this article, you are going to learn how to initialize a boolean variable using JAVA as a programming language. Don’t panic on hearing the word boolean because it is so simple and easy to learn.

Data Type?

What is a data type? Before entering into the real topic I want you to understand what a data type actually is. So a data type tells a compiler or an interpreter what type of data a user want to use in his program of assigning values.
The different types of data types included in java are
  1. integer
  2. float
  3. char
  4. boolean
A data type actually determines the variable to be initialised may be as an integer (int), float, character (char), boolean.
Integer is again classified into following types as byte, short, int, long; 
while float is also classified into the known types as float and double.
 
Each data type occupies memory of various bytes. For example, refer the following table
Data types, sizes, range of values that can be stored, and its default value

Byte -> 1 byte -> -128 to 127 -> 0

Short -> 2 bytes -> -32.768 to 32,767 -> 0

int -> 4 bytes -> -2,147,483,648 to 2,147,483,647 -> 0

long -> 8 bytes -> -9,223,372,036,854,775,808 to 9,223,372,036,854,807 -> 0

float -> 4 bytes -> 3.4e-0.38 to 3.4e+0.38  -> 0.0f

double -> 8 bytes -> 1.7e-308 to 3.4e+0.38 -> 0.0d

boolean -> 1 bit -> true/false -> false

char -> 2 bytes ->  - -> \u0000

Boolean Data type

When a variable is declared with Boolean data type, then it  indicates only two values. They are True or False, but not both at a time. 
 
Basically, boolean data type default value is false. This boolean data type can be used with variables, method parameters and its return types.
 
It is declared with boolean (small b) values which is rather termed as expression. 
Syntax

// 1. javaIsFun ias assigned with True value
boolean  javaIsFun = True;

//2. method parameters
public void setValue( boolean valid ) {
    //code
}
 
//3. method return type
public boolean isValue() {
    //code
}

Program

boolean demo = true;

if ( demo == true)   //  if ( !user ) 

{

System.out.println("it is true");

}

else 

{

System.out.println("it is false");

}

Output

it is True

 

140 points
4