1. Introduction
Boolean operators are essential components of programming languages, allowing developers to create logical expressions that evaluate to either true or false. In Java, boolean operators play a crucial role in controlling the flow of program execution, making decisions, and executing conditional statements. In this article, you’ll discover the different boolean operators in Java and learn how to use them in your programs.
2. What is a Boolean Operator?
A boolean operator is a logical operator used in programming to perform logical operations. These operations can apply to non boolean types, but will always return a boolean value. There are four categories of Boolean operators: equality operators, comparison operators, conditional operators, and logical operators.
3. Equality Operators
The equality operator is applicable to both primitive and object types.
3.1. Primitive Data Value Equality (== and !=)
The “==” operator determines whether two primitive a and b values are equal, i.e. whether they have the same primitive value.
int a = 3;
int b = 20;
boolean result = a == b; //result is false
The “!=” operator is the opposite of the “==” operator, i.e. it determines whether two primitive values a and b are unequal.
int a = 3;
int b = 20;
boolean result = a != b; //result is true
However, you should take care when making comparisons involving floating-point numbers. Since these are stored as approximations on a finite number of bits, you should avoid strict comparisons.
double a = 1.0 - 2.0/3.0;
double b = 1.0/3.0;
boolean result = a == b; //result is false
As you can see, the result of the above comparison is “false”. This is mathematically incorrect because 1 – 2/3 = 1/3.
One way to avoid this problem is to define a calculation precision.
double precision = 1e-10;
double a = 1.0 - 2.0/3.0;
double b = 1.0/3.0;
boolean result = (a - b) < precision; //result is true
3.2. Object Reference Equality (== and !=)
The equality operator on the objects tests not their contents but their respective references. To test the contents of objects, there’s another way, which we’ll see later: the equals()
method.
Equality Test(==)
Student student1 = new Student();//New Object
Student student2 = new Student();//New Object
Student student3 = student1;
boolean result = student3 == student1; //Returns true
Inequality Test(!=)
Student student1 = new Student();//New Object
Student student2 = new Student();//New Object
Student student3 = student1;
boolean result = student3 != student2; //Returns true
It’s important to note that the equality test is only possible if the objects being compared are cast-compatible.
Student student1 = new Student();//New Object
Student student2 = new Student();//New Object
Student student3 = student1;
String notStudentCompatible = "This is String cannot be cast to a Student object";
boolean result = student3 != notStudentCompatible; //Compile-time error
4. Comparison Operators
The following table summarizes the Relational Operators:
Operators | Meaning |
---|---|
a < b | is a less than b? |
a <= b | is a less than or equal to b? |
a > b | is a greater than b? |
a >= b | is a greater than or equal to b? |
All relational operators are binary and operands are numerical expressions.
These operators are nonassociative. This means that the following code will not compile.
int a = 4, b = 5, c = 9;
boolean result = a < b < c; //Compile-time error
You must rewrite the code as follows:
int a = 4, b = 5, c = 9;
boolean result = a < b && b < c;//Compile-time error
5. Conditional Operators (&& and ||)
In Java, the logical AND (&&) and logical OR (||) operators are used for boolean expressions to combine multiple conditions.
Operators | Meaning |
---|---|
a && b | Returns true if both a and b are true, otherwise returns false |
a || b | Returns true if either or both a and b are true, otherwise returns false |
These are short-circuit operators, meaning that the right operand is only evaluated if necessary.
Running the code below will print 0 to the console. This shows that the second condition i++ < 20
was not evaluated.
int i = 0;
if(i>1 && i++ < 20){
doSomething();;
}
System.out.println(i);//Prints: 0
Below is a table summarizing the possible values of the result of the operation as a function of the operands a and b.
a | b | a && b | a || b |
---|---|---|---|
true | true | true | true |
true | false | false | true |
false | true | false | true |
false | false | false | false |
6. Boolean Logical Operators (&, |, !, and ^)
The operators that fall into this category are: logical complement (!), logical AND, logical inclusive OR, logical exclusive OR also known as XOR.
Let a and b be two Boolean expressions. The table below presents the result of applying the different operators.
a | b | !a | a & b | a ^ b | a | b |
---|---|---|---|---|---|
true | true | false | true | false | true |
true | false | false | false | true | true |
false | true | true | false | true | true |
false | false | true | false | false | false |
Unlike conditional operators, logical operators necessarily evaluate both operands.
int i = 0;
if(i>1 & i++ < 20){
doSomething();
}
System.out.println(i);//Prints: 1
The code below will print 1 to the console, meaning that the second expression (i++ < 20) was evaluated even if it was not strictly necessary.
7. Boolean Logical Compound Assignment Operators (&=, |=, and ^=)
There are also compound logical operators on which we will not spend much time. All you need to know can be summarized here:
a &= b
is equivalent toa = a & b
a |= b
is equivalent toa = a | b
a ^= b
is equivalent toa = a ^ b
8. Precedence and Associativity Rules
Relational operators are nonassociative. They have lower precedence than the arithmetic operators, but higher precedence than the assignment operators.
You can read more about Operator Precedence here.
9. Conclusion
You’ve just scratched the surface of boolean operators in Java! These little tools are like magic spells, helping your code compare things and make decisions. Now that you’ve got the hang of them, you can use them to build all sorts of cool stuff.
10. References
1) OCP Oracle Certified Professional Java SE 17 by Khalil A. Mughal and Vasily A. Strelnikov
2) Oracle Java Documentation
Pingback: Java Operators Decoded: Essential Tools for Crafting Robust Code