Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Boolean data type

Boolean data type can have only one of two possible values, true or false. Boolean data type can be declared as follows.

boolean bln = <value>;

Where value can either be true or false.

Boolean data type is mainly used in conditional statement which enables program to do different things depending upon the value of the variable.

Boolean data type example

 
  1. public class FindNumber {
  2.  
  3.  public static void main(String args[]){
  4.  
  5.   boolean found = false;
  6.   //number to search
  7.   int find = Integer.parseInt(args[0]);
  8.  
  9.   //range of numbers
  10.   int range = Integer.parseInt(args[1]);
  11.  
  12.   for(int i=0; i < range; i++){
  13.    if(i == find){
  14.     found = true;
  15.     break;
  16.    }
  17.   }
  18.  
  19.   //if number found print message
  20.   if(found){
  21.    System.out.println("Number found in given range.");
  22.    
  23.   //if number not found
  24.   }else{
  25.    System.out.println("Number not found in given range.");
  26.   }
  27.  
  28.  }
  29. }
Your rating: None Average: 5 (1 vote)
Share this
Anonymous's picture

(No subject)

Post new comment

5 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.