how to use boolean data type in Java

By: Lakshmi  

Java has a simple type, called boolean, for logical values. It can have only one of two possible values, true or false. This is the type returned by all relational operators, such as a < b. boolean is also the type required by the conditional expressions that govern the control statements such as if and for.

Here is a program that demonstrates the boolean type:

// Demonstrate boolean values.
class BoolTest {
public static void main(String args[]) {
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
// a boolean value can control the if statement
if(b) System.out.println("This is executed.");
b = false;
if(b) System.out.println("This is not executed.");
// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}

The output generated by this program is shown here:

b is false
b is true
This is executed.
10 > 9 is true

There are three interesting things to notice about this program. First, as you can see, when a boolean value is output by println( ), "true" or "false" is displayed. Second, the value of a boolean variable is sufficient, by itself, to control the if statement. There is no need to write an if statement like this:

if(b == true) ...

Third, the outcome of a relational operator, such as <, is a boolean value. This is why the expression 10 > 9 displays the value "true." Further, the extra set of parentheses around 10 > 9 is necessary because the + operator has a higher precedence than the >.




Archived Comments

1. CaseyOffer
View Tutorial          By: CaseyOffer at 2017-04-15 17:09:38

2. You can clear many of your doubts regarding boolean data types in Core Java through Merit Campus, vi
View Tutorial          By: Merit Campus at 2016-02-16 10:43:07

3. You can clear many of your doubts regarding characters in Core Java through Merit Campus, visit: htt
View Tutorial          By: Merit Campus at 2016-02-16 10:40:19

4. thanksssssssssssss
View Tutorial          By: gajender sharma at 2015-02-02 09:15:05

5. thanks a lot
View Tutorial          By: Uzzal biswas at 2014-11-04 02:42:54

6. looks like you have copied from w3schools.,
View Tutorial          By: Prakash at 2014-07-04 06:37:10

7. Nice stuff u guyzz are sharing
View Tutorial          By: kunal sharma at 2013-11-19 03:45:08

8. how to return the boolean value. what is the data type to be mentioned?
public _______ getID

View Tutorial          By: karthick at 2013-02-07 10:21:28

9. A phone is considered a good phone if all of the following conditions are satisfied:-


View Tutorial          By: harish at 2012-12-24 06:59:04

10. please would someone help me create a simple java billing program??? .... help me plxxxx!
View Tutorial          By: christopher at 2012-12-05 00:32:50

11. thank you, this best for student like us,
View Tutorial          By: Srinath at 2012-05-12 15:04:03


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial