Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0021 boolean

Java has a boolean type for logical values. It can have only one of two possible values, true or false. This is the type returned by all relational operators. Here is a program that demonstrates the boolean type: public class Main { public static void main(String args[]) { boolean boolVariable; boolVariable = false; System.out.println("b is " + boolVariable); boolVariable = true; System.out.println("b is " + boolVariable); if (boolVariable) { System.out.println("This is executed."); } boolVariable = false; if (boolVariable) { System.out.println("This is not executed."); } System.out.println("10 > 9 is " + (10 > 9)); } } Output: b is false b is true This is executed. 10 > 9 is true