Understanding boolean data type – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #JAVA#3850    siteicon   siteicon  

Understanding boolean data type

The java boolean data type declaration and its features.

Learning Objectives

  • Declaration and initialisation of boolean type.
  • Non-convertibility of boolean type to other types.

Source Code

TC++ #3850

Source Code

class BooleanType
{
public static void main(String[] args)
{
boolean b=true;
//int x=b;
if(b) System.out.println(b);
if(b==true) System.out.println(b);
}
}

Test it !

Run Output

true
true

Code Understanding

boolean b=true;
Here we declare boolean data type b which is assigned a true status. true in java is just a reserved word to be used as boolean literal. The opposite of this is false.

//int x=b;
This line has been commented. If we un-comment this the code will not run as boolean type can not be converted to other type

if(b) System.out.println(b);
The boolean type can be directly checked in if conditions without using any comparison operator.

if(b==true) System.out.println(b);
However the use of comparison operator with boolean type is also allowed.

 


Suggested Filename(s): BooleanType.java



Share

sunmitra| Created: 19-Mar-2018 | Updated: 19-Mar-2018|






Back