What is Java Booleans ? How it works ?

Java Booleans Tutorial


Boolean Expressions in Java

 

By Doug Lowe

A boolean type can have one of two values: true or false. A boolean is used to perform logical operations, most commonly to determine whether some condition is true. For example:

boolean enrolled = true;
boolean credited = false;

Here, a variable named enrolled of type boolean is declared and initialized to a value of true, and another boolean named credited is declared and initialized to false.

In some languages, such as C or C++, integer values can be treated as a boolean, with 0 equal to false and any other value equal to true. Not so in Java. In Java, you can’t convert between an integer type and a boolean type.

Boolean expression is a Java expression that, when evaluated, returns a Boolean value: true or false. Boolean expressions are used in conditional statements, such as if, while, and switch.

The most common Boolean expressions compare the value of a variable with the value of some other variable, a constant, or perhaps a simple arithmetic expression. This comparison uses one of the following relational operators: