Pavlo Plynko
Java Developer at CodeGym

# Java Boolean

The word “boolean” in the context of Java language can be used in different, albeit very related, meanings. It could be:
• boolean primitive type or boolean variable of this type
• Java Boolean class or Boolean wrapper object
• Boolean expression, boolean value, some condition
• Boolean operators
In this article, we are going to cover all of these options and explain what concepts underlie Boolean expressions.

## What is a boolean in general sense

The concept of a Boolean expression came from mathematics, or rather, from mathematical logic. A Boolean expression in propositional algebra is an expression that can be said to be true or false. For example:
“Snow is white” "Crocodiles can fly" “2 + 2 = 4” “1 + 1 = 21”
At the same time, "2" or "snow" are not Boolean expressions.

## Java boolean primitive data type and boolean variables

Talking of a boolean in Java, first it's most likely a boolean primitive data type and boolean variables of this type. As you probably already guessed, variables of this type can only take two values, true and false. Java has pretty strict restrictions: a boolean in Java can’t be converted to any other data type, and vice versa. In particular, boolean in Java isn’t an integral type, and integer values can’t be used instead of booleans. Here is an example of directly setting a boolean type:
``````
boolean myBoolean; //boolean variable
myBoolean = false;
boolean myBoolean1 = true; //another boolean variable
``````
Here we have 2 boolean variables. Let's write a small program with an example of using a boolean type:
``````
//boolean variable example
public class BoolTest {

public static void main(String[] args) {
boolean myBoolean = false;
System.out.println(myBoolean);
}
}
``````
This program prints “false” to the console. By the way, a boolean variable is set to false by default, but Java doesn't allow you to work with uninitialized local variables.

## Boolean expressions in Java

In addition to explicitly initializing a boolean variable to true or false, the boolean data type is implicitly used in many places. Just as the result of any addition of numbers will be a number, the result of any comparison will be true or false, that is, it will be of a boolean type. This means that, in addition to directly specifying a boolean value through the boolean variable assignment statement, boolean values ​​result from various comparisons, such as 5 > 2, and are used primarily in conditional and loop statements. Here is an example of such a use of boolean type:
``````
public class BoolTest {

public static void main(String[] args) {
boolean myBoolean = false;
int a = 5;
int b = 7;
System.out.println(a < b);
System.out.println(0 > 7);
System.out.println(myBoolean == false);
}
}
``````
The output is:
true false true
In the case of a < b, the < operator compared the expression on the left with the expression on the right. We displayed the result of the comparison on the screen. Since 5 < 7 (the statement is true), the value true will be printed to the console. In the second case, we display a direct comparison of zero and seven, and in the third, we ask if the value of the variable myBoolean is false. Since this is the case, we output the value true. In fact, to build boolean expressions in Java, we can use any comparison operators:
comparison operators Java operator Operation example The result of operation
Less < a < b true if a is less than b otherwise false
Greater > a > b true if a is greater than b, otherwise false
Less than or equal <= a <= b true if a is less than b or they are equal, otherwise false
Greater or equal >= a >= b true, if a greater or equal to b, otherwise false
Equal == a == b true, if a is equal to b, otherwise false
Not equal != a != b true, if a is not equal to b, otherwise false

## Where boolean values are used

Boolean values and conditional expressions are very often used in the conditions of branch statements, ternary operators, and loops. In fact, their use is based on checking certain Boolean expressions. For example:
``````
public class BoolTest2 {
public static void main(String[] args) {
int i = 0;
while (i <= 10)
{
System.out.println(i);
i++;
}
}
}
``````
This program prints out a sequence of integers and increments them by one as long as the condition in the brackets after the while is met. That is, while the expression i <=10 is true.

## Java boolean operators. Building Boolean Expressions with Boolean Operators

The following logical (or boolean) operations are available in Java:
• Logical negation, it is also NOT or inversion. In Java, denoted by the symbol ! before expression.

• Logical and, it is also AND or conjunction. Denoted by the symbol & between the two expressions to which it is applied.

• Logical or in Java, it is also OR, it is also disjunction. In Java, denoted by the symbol | between two expressions.

• Exclusive or, XOR, strict disjunction. In Java, it is denoted by the symbol ^ between two expressions.

• In Java, logical operators include the conditional or, denoted as ||, as well as the conditional and, &&.

Let's have a look at the table with a brief description of each of the Java boolean operators, and below we will describe them in more detail and give code examples. By “operands” in the table, we mean the logical expression or variables to which the operator is applied.
``````
a | b == true
``````
Boolean Java operator Name Type Description Example
! Logical “not” (negation) unary !x means “not x”. Returns true if x is false. Returns false if x is true.
``````
boolean x = true;
``````

then

``````
// !x == false``````
& Logical "and" (and, logical multiplication) binary (a & b) returns true if both a and b are true.
``````
a = true;
b = false;
``````

then

``````
a & b == false
``````
| Logical OR (logical addition) binary (a | b) returns true if a or b or both of them are true.
``````
a = true;
b = false;
``````

then

^ Logical exclusive OR (XOR) binary (a ^ b) returns true, if only one of operands (a or b) is true. Returns false, if a and b both are true or false simultaneously. In fact if a is not equal to b it returns true.
``````
a = true;
b = false;
``````

then

``````
a ^ b == true
``````
&& Conditional AND (abbreviated logical AND) binary a && b It’s the same as a & b, but if a is false, the operator just returns false without checking b.
|| Conditional OR (abbreviated logical OR) binary a || b is the same as a | b, but if a is true, the operator just returns true without checking b.
Note that in Java, the operators &, | and ^ also apply to integers. In this case, they work a little differently and are called bitwise (or bitwise) logical operators. Let's take an example and display several logical expressions composed using logical operators.
``````
public class BoolTest2 {
public static void main(String[] args) {
int a = 5;
int b = 7;
boolean myBool1 = true;
boolean myBool2 = false;
System.out.println(myBool1&myBool2);
System.out.println(myBool1|myBool2);
System.out.println(!myBool1);
System.out.println((a > b) & !myBool1 | myBool2);
}
}
``````
Here is the output:
false true false false
In fact, you can make very complex logical constructions using logical operators. for example
(a<!b)&(q+1 == 12)^(!a | c & b > 1 + b)|(q ^ a > !b)
If all variables are initialized, such constructions will work. However, you should not abuse them, they make it difficult to read the code. Nevertheless, it is very useful to deal with such logical constructions. Try to make logical expressions with other logical operators given in the table.

## Precedence of logical operations

As in mathematics, in programming, operators have a specific order of execution if they occur in the same expression. Unary operators have advantages over binary ones, and multiplication (even logical) over addition. Here are logical operators placed in the list of topics higher, the higher their priority:
• !

• &

• ^

• |

• &&

• ||

## Java Boolean wrapper

In Java, each primitive type has a "brother", a wrapper class (Wrapper). A wrapper is a special class that stores the value of a primitive inside. However this is a class, so you can create instances (objects) of it. These objects store the necessary values of primitives inside, while they will be real objects. Java boolean primitive type has a wrapper Java Boolean (with capital B) class. Boolean Class objects are created just like any other:
``````
Boolean b = new Boolean(false);
``````
Java Boolean class has useful methods. One of the most interesting of these is the parseBoolean method. static boolean parseBoolean(String s) method parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”. Otherwise it returns false.

### parseBoolean method example

``````
public class BoolTest2 {

public static void main(String[] args)
{
System.out.println(Boolean.parseBoolean("True"));
System.out.println(Boolean.parseBoolean("TRuE"));
System.out.println(Boolean.parseBoolean("False"));
System.out.println(Boolean.parseBoolean("here"));

}
}
``````
The output is:
true true false false